Ajax Call保持失败而没有错误

时间:2014-12-02 23:36:37

标签: javascript php jquery ajax

所以我的ajax调用工作正常,我在Web检查器旁边看到绿灯,我看到正在发送的参数和JSON对象被返回。但是,它会自动进入ERROR。我添加了一些错误处理并且已经没有了想法..

有人看到了吗?

的Javascript

    function getMoreOlder(id) { 
        $.ajax({
            url: "getmore.php",
            type : "POST",
            dataType : "json",
            data: {
                postid : id,
                type: "later",
            },
           success: function(response){
               alert("Success");
              for (var i=0; i<json.length; i++) {
               //     alert("Loop");
                    }
          },
          error: function(jqXHR,error, errorThrown){
                   if(jqXHR.status&&jqXHR.status==400){
                        alert(jqXHR.responseText); 
                   }else{
                       alert("Something went wrong");
                   }
          }   
         });
    }

PHP脚本

        $id = $_POST['postid'];
        $type = $_POST['type'];

        if($type == "later") {
        $sql = "CALL `sp_BlogPosts_PageByPostID`('" . $id ."')";
        $exec = mysqli_query($conn, $sql);

        while($fetch = mysqli_fetch_assoc($exec)) { 
           $obj = array(
             "PostID" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['PostID']))),  
             "Title" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['title']))),     
             "Content" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['content']))),     
             "Email" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['email']))),    
             "Username" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['username']))),    
             "Created_At" => stripslashes(substr(trim(mysqli_real_escape_string($conn, $fetch['created_at'])), 0 , 10)));
              echo json_encode($obj);
        }

使用示例

  <button onClick="getMoreOlder(1113);"> Load More Posts </button>

返回示例

    {"PostID":"1177","Title":"Off track","Content":"Hi This is a Post", "Email":"me@yahoo.com", "Username":"Abc123", "Created_At":"12-2-14"}

要点

  • 正在POST中发送正确的变量

  • 在POST

  • 中返回正确的信息
  • 始终返回错误出错了!

  • Web Inspector显示200个POST请求代码

1 个答案:

答案 0 :(得分:2)

问题是你不断地回显出json_encode,这会使json输出无效。

例如:

{"PostID":"1177","Title":"Off track"}{"PostID":"1177","Title":"Off track"}

这是无效的json。因此,将所有数据放入数组/对象,然后回显json_encode

$objArr = array();
while($fetch = mysqli_fetch_assoc($exec)) { 
    $objArr[] = array(
        "PostID" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['PostID']))),  
        "Title" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['title']))),     
        "Content" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['content']))),     
        "Email" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['email']))),    
        "Username" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['username']))),    
        "Created_At" => stripslashes(substr(trim(mysqli_real_escape_string($conn, $fetch['created_at'])), 0 , 10))
    );          
}

echo json_encode($objArr);

或者按照评论中的建议一次获取所有数据并回显

echo json_encode($exec->fetch_all(MYSQLI_ASSOC));

你的for循环也使用错误的变量

for (var i=0; i<json.length; i++) {

应该是

for (var i=0; i<response.length; i++) {