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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c