如何处理带有PHP后端的JSON错误?

我以前从未使用过JSON,所以运行得很不顺利......

我有一个返回JSON数组的PHP脚本,脚本返回:{"items":1000,"mitems":0,"donations":0,"total":1000}。此外,该脚本还将Content-Type设置为application/json。

这是处理该响应的前端javascript:

function ajax(){
    $.ajax({
         url: '../ajax/goal_ajax.php',
         dataType: 'json',
         success: function( data ){
          // success! :D
          alert('success');
         }, error: function( data ){
          // data.responseText is what you want to display, that's your error.
          alert(data.responseText);
         }
    })
    //progressBar.set('value',data.total);
    //document.getElementById('txtCDInfo').innerHTML=txt;
} 

调用该函数时,我收到以下警报消息:{"items":1000,"mitems":0,"donations":0,"total":1000}。

如果一切成功,我应该收到一条警告,提示success对吗?

有人可以告诉我这里发生了什么吗?

This is the least documented thing in jquery what you need to do is alert the actual error in order to debug it. so do the following:

function my_ajax(){
    $.ajax({
         url: '/ajax/goal_ajax.php',
         dataType: 'json',
         success: function( data ){
          // success! :D
          alert('success');
         }, error: function(jqXHR, textStatus, errorThrown){
          // data.responseText is what you want to display, that's your error.
          alert(jqXHR+","+textStatus+","+errorThrown);
         }
    })
    //progressBar.set('value',data.total);
    //document.getElementById('txtCDInfo').innerHTML=txt;
} 

So two things I've done:

Change the name of the function (ajax is kinda a bad name :S) and improved the error reporting.

You should be getting the alert "success" yes. So something is going wrong.

EDIT:

Just noticed another thing, I dont think "../" would be a great way to reference the url, usually its either "/foo/ajax" which will allow you to use this function on any page.

Are you defining the MIME type in your HTTP response?

Try adding a Content-type header to the output of your script.

<?php
...
    $output = json_encode(...);
    header('Content-type: application/json');
    echo $output;
...
?>

You can also try using the mimeType parameter in your $.ajax() call to override the response type.

http://api.jquery.com/jQuery.ajax

Are you running your PHP scripts under Apache or on their own (chmod u+x on the script)? You probably need to use a PHP framework such as Zend, CodeIgniter or CakePHP and define a controller action that handles your AJAX request to have a proper HTTP response.

As the guy above me said, you should be using the json_encode(); function in PHP then echo'ing that output. Make sure you encode an array though:

    <?
      $send = array(
                  'items' => '1000',
                  'mitems' => '0', 
                  'donations' => '0', 
                  'total' => '1000',
                );

      echo json_encode($send);
    ?>

It could be that your PHP script returns an error status code and even though it prints out the correct result, it still fails. I tested your scripts on my system and I got the 'success' alert. Then I changed my PHP script to the following:

<?php
header('Content-type: application/json', true, 401); 
echo '{"items":1000,"mitems":0,"donations":0,"total":1000}';
?>

Note that the third parameter of the header function sets the http response code to 401 - Even though the correct output is sent back to the client; officially, the request has failed because of that status code. After running this code, I got the exact same problem as you.

So in summary, there might be something in your script which is causing a non-fatal error which doesn't show in the output of the script.