简体   繁体   English

$ .ajax发布成功后的jQuery Ajax JSON错误报告

[英]JQuery Ajax JSON error reporting on $.ajax post success

EDIT: I fixed the errors with the PHP that have been mentioned in Answers but I still have the same problem. 编辑:我修复了答案中已经提到的PHP错误,但是我仍然遇到相同的问题。

I'm trying to display an error message if my POST request fails, and to show a success message if it doesn't fail. 如果我的POST请求失败,我尝试显示一条错误消息,如果没有失败,则尝试显示成功消息。 It currently shows the success message correctly, but nothing happens if I make the sql fail, even in console. 它当前正确显示成功消息,但是即使我在控制台中使sql失败,也不会发生任何事情。

JQUERY: // I have removed unnecessary code JQUERY://我删除了不必要的代码

        $("#form").validate({
        ignore:'',
        errorPlacement: function(error, element) {
            error.insertAfter(element.parent()); // <- make sure the error message appears after parent element (after textbox!)
        },
        rules: {
        },
        messages: {
        },
        submitHandler: function(form) { 
            var datastring = $("#form").serialize();
            $.ajax({
            type: "POST",
            url: "myscript.php",
            data: datastring,
            dataType:"json",
            success:  function (response) {
                if(response.status === "success") {
                    window.alert(response.comment);
                } else if(response.status === "error") {
                    window.alert(response.comment);
                }
             }
        }); // close $.ajax
    } // close submit handler 
}); //end validate

PHP: PHP:

    if($row = sasql_fetch_array($insert))
{   
    //success message 
    $arr = array ('status'=>'success','comment'=> $custid );
    echo json_encode($arr);


}  else {
    //error message
    $arr = array ('status'=>'error','comment'=>'There has been an error. Please try again.');
    echo json_encode($arr);
}

Does anyone have any ideas about what the issue could be here? 是否有人对这里可能出现的问题有任何想法? Like I said, if I add console.log(JSON.stringify(response)); 就像我说的,如果我添加console.log(JSON.stringify(response)); just after the success function, nothing at all is returned on the error array. success函数之后,错误数组上什么也不返回。 Thanks 谢谢

Problem in your php file. 您的php文件中的问题。 You got syntax error in failure part. 在失败部分中出现语法错误。 But not in success. 但是没有成功。 You used unwanted } and concatenate string using + instead of . 您使用了不需要的},并使用+代替了字符串。 operator. 操作员。 Also remove the empty spaces near the array. 同时删除阵列附近的空白区域。

    if($row = sasql_fetch_array($insert))
{   
    //success message 
    $arr = array ('status'=>'success','comment'=> $custid );
    echo json_encode($arr);
}

}  else {
    //error message
    $arr = array ('status'=>'error','comment'=>'There has been an error. Please try again.'+$custid);
    echo json_encode($arr);
}

should be 应该

    if($row = sasql_fetch_array($insert))
{   
    //success message 
    $arr = array('status'=>'success','comment'=> $custid );
    echo json_encode($arr);

}  else {
    //error message
    $arr = array('status'=>'error','comment'=>'There has been an error. Please try again.'.$custid);
    echo json_encode($arr);
}

If you look at this line: 如果您看这行:

$arr = array ('status'=>'error','comment'=>'There has been an error. Please try again.'+$custid);

You'll see that you're using + instead . 您会看到您使用的是+ . , which is the concatenation operator in PHP . ,它是PHP中串联运算符

it is the correct php code 这是正确的PHP代码

if($row = sasql_fetch_array($insert))
{   
    //success message 
    $arr = array ('status'=>'success','comment'=> $custid );
    echo json_encode($arr);

}  else {
    //error message
    $arr = array ('status'=>'error','comment'=>'There has been an error. Please try again.' . $custid);
    echo json_encode($arr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM