can't receive response from server in slim

Vogel_guo

We're using php-framework "slim" to build an e-shop. Now we are meeting a problem that we can send a request to server and modify the database(we checked table and it is changed indeed), whereas web end can't get the response from the database(iOS and android end can both get it). Here is the part of the code which sends the request, updates database and gets the response:

$app->post('/tblUser', function($request, $response, $args) {
   get_tblUser_id($request->getParsedBody());
});
function get_tblUser_id($data)
{
   $db = connect_db();
   $sql = "update  tblphoneverify set dtCreate = NOW() where strPhone = $data[phone]";
   $db->query($sql);
   $updateId = $db->affected_rows;
$db = null;
    $msg = array(
        'stat' => '',
        'msg' => ''
    );
    $msg['stat'] = '1';
    $msg['msg'] = 'registration success';
    return json_encode($msg);
}

then this ajax segment triggers the click event to execute post and receives the state of the result:

$(function(){
  $("#getcheck").click(function(){
    $.ajax({
      type:"post",
      url:"http://192.168.1.108/blue/public/tblUser",
      data: {"phone":"13331111111"},
      dataType:"json",

      //async:false,
      contentType: "application/x-www-form-urlencoded",

      success:function(data){
        alert(1);
      },
      error:function(XMLHttpRequest, textStatus, errorThrown){
        alert(XMLHttpRequest.readyState);
        alert(XMLHttpRequest.status);
        alert(XMLHttpRequest.statusText);
        alert(XMLHttpRequest.responseText);
        alert(textStatus);
        alert(errorThrown);
      }
    })
  })
})

the code always skips the "success" part and jumps to "error" directly. So what is wrong with our code? Thanks in advance.

Rob Allen

You should send a response from a route callable. Don't json_encode yourself, instead let Slim do it.

Firstly, return an array from get_tblUser_id:

function get_tblUser_id($data)
{
    $db = connect_db();
    $sql = "update tblphoneverify set dtCreate = NOW() where strPhone = $data[phone]";
    $db->query($sql);
    $updateId = $db->affected_rows;
    $db = null;
    $msg = array(
        'stat' => '',
        'msg' => ''
    );
    $msg['stat'] = '1';
    $msg['msg'] = 'registration success';
    return $msg;
}

Note that you have a SQL injection vulnerability here. Change the SQL to something like this:

   $sql = "update tblphoneverify set dtCreate = NOW() where strPhone = ?";
   $db->query($sql, [$data[phone]]);

Next, you need to send a response as JSON from the route callable. Slim has a method to do this:

$app->post('/tblUser', function($request, $response, $args) {
    $msg = get_tblUser_id($request->getParsedBody());

    return $response->withJson($msg);
});

Slim will now send back your the msg array with the correct content-type header set, which should help your JavaScript to decode it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

can't receive server full response

From Dev

can't receive server full response

From Dev

Can not receive a response from a remote server

From Dev

Java Proxy - Can't receive response from host/Socket issue

From Dev

ajax 304 not modified can't receive response

From Dev

Why proxy server cannot receive response from server

From Dev

JAVA UDP Server Can't receive Packet

From Dev

Who will receive the response from server if calling thread is not active?

From Dev

TCP can't receive text messages after sending a file from server in Java

From Dev

C# TCP Server-Client: can't receive data from the client in an infinite loop

From Dev

Can't receive plain text from my Java Server app in Google Chrome

From Dev

Can't receive value from input field

From Dev

Can't receive an answer from python socket

From Dev

Java can't get client or server response

From Dev

Why I can't receive files on nodeJS server?

From Dev

Can't launch simple Sinatra app using rackup and jRuby (no response from web server)

From Dev

Can't get Java's GZIPInputStream to read "gzip" response from server when using "Range" header

From Dev

Can't instanciate Slim object

From Dev

How can I realtime receive message from server

From Dev

Server doesn't receive more messages from client

From Dev

Can we use ssh to connect to a web server and then send a http request and receive a http response?

From Dev

Jquery - Can't remove() from AJAx response

From Dev

Can't post response from AsyncTask to MainActivity

From Dev

Can't get item from a JSON response

From Dev

Android Firebase - Can't receive proper JSON from Firebase snapshot

From Dev

Can't receive images from Parse using Collection View

From Dev

Why I can't receive messages from GCM on android device

From Dev

Can't receive json data from PHP in Chrome and Opera

From Dev

Why can't my site receive the claims from ADFS 2.0

Related Related

  1. 1

    can't receive server full response

  2. 2

    can't receive server full response

  3. 3

    Can not receive a response from a remote server

  4. 4

    Java Proxy - Can't receive response from host/Socket issue

  5. 5

    ajax 304 not modified can't receive response

  6. 6

    Why proxy server cannot receive response from server

  7. 7

    JAVA UDP Server Can't receive Packet

  8. 8

    Who will receive the response from server if calling thread is not active?

  9. 9

    TCP can't receive text messages after sending a file from server in Java

  10. 10

    C# TCP Server-Client: can't receive data from the client in an infinite loop

  11. 11

    Can't receive plain text from my Java Server app in Google Chrome

  12. 12

    Can't receive value from input field

  13. 13

    Can't receive an answer from python socket

  14. 14

    Java can't get client or server response

  15. 15

    Why I can't receive files on nodeJS server?

  16. 16

    Can't launch simple Sinatra app using rackup and jRuby (no response from web server)

  17. 17

    Can't get Java's GZIPInputStream to read "gzip" response from server when using "Range" header

  18. 18

    Can't instanciate Slim object

  19. 19

    How can I realtime receive message from server

  20. 20

    Server doesn't receive more messages from client

  21. 21

    Can we use ssh to connect to a web server and then send a http request and receive a http response?

  22. 22

    Jquery - Can't remove() from AJAx response

  23. 23

    Can't post response from AsyncTask to MainActivity

  24. 24

    Can't get item from a JSON response

  25. 25

    Android Firebase - Can't receive proper JSON from Firebase snapshot

  26. 26

    Can't receive images from Parse using Collection View

  27. 27

    Why I can't receive messages from GCM on android device

  28. 28

    Can't receive json data from PHP in Chrome and Opera

  29. 29

    Why can't my site receive the claims from ADFS 2.0

HotTag

Archive