duandie0921 2017-01-13 08: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-24 06: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.

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

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法