duandie0921 2017-01-13 00:33
浏览 45
已采纳

无法从苗条的服务器接收响应

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.

展开全部

  • 写回答

1条回答 默认 最新

  • dongzuo7166 2017-01-23 22:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部