我找不到我的错误;从Ajax请求返回空

时间:2014-11-19 07:07:35

标签: php mysql ajax response

这是我的来源;洛尔

 $(document).on('click', '#findFirstUndefined', function (event) {
       event.preventDefault();
       $.ajax({
           type: 'post',
           url: './custom/tool.node.php',
           dataType: 'text',
           data: {
               m: 't',
               f: 'fu'
           },
           beforeSend: function () {
               console.log('Request find first undefined node..');
           },
           success: function (data) {
               console.log('Request success.');
               console.log(data);
           },
           error: function () {
               console.log('Request failure.');
           }
       });
   });
    // Following is tool.node.php
   if ($_POST['m'] === 'n') {
       if (createNode($db)) echo $db - > insert_id;
       else echo 'false';
   }
    // ..
    else if ($_POST['m'] === 't') {
       if ($_POST['t'] === 'fu') {
           // Find first undefined node..
           $sql = "select `id` from `v3_node` where `type`=0 limit 0,1;";
           $rst = $db - > query($sql);
           // Initialize array for json string
           $return = array('rstExist' = > null);
           if ($rst - > num_rows === '0') {
               // Undefined Node NOT exist.
               $return['rstExist'] = false;
           } else {
               // Undefined Node exist.
               $return['rstExist'] = true;
               // Find 20 items around first undefined node.. 
               $row = $rst - > fetch_assoc();
               $sql = "select * from `v3_node` where `id`>=".($row['id'] - fmod($row['id'], 20) + 1).
               " limit 0, 20;";
               $rst = $db - > query($sql);
               $return['node'] = array();
               while ($row = $rst - > fetch_assoc()) {
                   array_push($return['node'], $row);
               };
           };
           echo json_encode($return);
       };
   };

我想要的只是通过ajax获取行并且响应是空的..其中没有。我甚至无法找到我的错误,所以需要帮助!

如果我将dataType属性更改为' json',控制台会打印Request Failure这是错误回调;那是为什么???

1 个答案:

答案 0 :(得分:0)

在此行data: { m: 't', f: 'fu' },之后。它将执行else语句,因为m == 't'

else if($_POST['m'] === 't')

在这个else语句中,它必须是t == 'fu',但这不是真的,因为$_POST['t']不存在。我认为这需要f而不是t。所以改变这一行:

if($_POST['t'] === 'fu') {

if($_POST['f'] === 'fu') {