dongque20030402
2015-02-12 05:29
浏览 127

jQuery .ajax调用返回JSON ParseError甚至认为JSON似乎是正确的?

I'm getting a ParseError even though my JSON validates on jsonlint.com.

Here is the jQuery code:

$.ajax({
    url: path,
    type: 'GET',
    data: {},
    cache: false,
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',                  
    success: function (data) {
        var a = 'breakpoint here doesn't activate';
    },
    error: function (x, y, z) {
        var b = 'code execution stops at a breakpoint here.';
    }
});

Here is the PHP code that is being called:

function getAllAnswersToHitViaAjax($theJobName) {
    $testData[0] = 'testing123';
    $encodedData = json_encode($testData);
    echo $encodedData;
    return;
}

This comes back to a breakpoint set in the error: function of my .ajax call. Parameter Y is set to "parseerror", and x.responseText =

["testing123"] 

I've been looking into this for hours so far. I've looked at many relevant StackOverflow posts, but none have solutions that work in this case.

How can I get a success response from this .ajax call?

Thanks very much in advance to all for any info.

图片转代码服务由CSDN问答提供 功能建议

即使我的JSON在jsonlint.com上验证,我也会收到ParseError。

这是jQuery代码:

  $ .ajax({
 url:path,
 type:'GET',
 data:{},\  n cache:false,
 dataType:'json',
 contentType:'application / json; charset = utf-8',
 success:function(data){
 var a ='breakpoint here not activated  '; 
},
错误:函数(x,y,z){
 var b ='代码执行在此处的断点处停止。'; 
} 
}); 
  <  / pre> 
 
 

以下是被调用的PHP代码:

  function getAllAnswersToHitViaAjax($ theJobName){
 $ testData [0] ='  testing123'; 
 $ encodedData = json_encode($ testData); 
 echo $ encodedData; 
 return; 
} 
   
 
 

这回到断点 设置错误:我的.ajax调用函数。 参数Y设置为“parseerror”,x.responseText =

  [“testing123”] 
   
 
 

到目前为止,我一直在研究这个问题。 我已经查看了许多相关的StackOverflow帖子,但没有一个解决方案适用于这种情况。

如何通过此.ajax调用获得成功响应?

非常感谢所有人提供任何信息。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dongxi5505 2015-02-12 06:15
    已采纳

    There's nothing visibly wrong with your code, and it works fine when I try it on my local machine. However, your comment above is a big clue:

    I just looked at the z param in the Safari console, and found this:"undefined is not a function (evaluating 'JSON.parse(a+"")')" How could that be happening?

    It could happen if some code somewhere uses "JSON" as a global variable name, hiding the built-in window.JSON object.

    打赏 评论
  • duanchai0028 2015-02-12 05:34

    Please use

     $.ajax({
        url: path,
        type: 'GET',
        cache: false,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',                  
        success: function (data) {
            var a = 'breakpoint here doesn\'t activate';
        },
        error: function (x, y, z) {
            var b = 'code execution stops at a breakpoint here.';
        }
    });
    

    and in your PHP CODE Paste this code.

    function getAllAnswersToHitViaAjax($theJobName) {
        $testData[0] = 'testing123';
        $encodedData = $testData;
        echo json_encode($encodedData);
        exit;
    }
    

    May be after digging more in your code I assume that in your PHP code you were not passing data properly. Please use exit or die method to pass data back to ajax. Please check.

    打赏 评论
  • duanleixun2439 2015-02-12 05:46

    Check for Notice or Warning in your php code, if their is any then remove that and then try. Hop this will help you.

    打赏 评论
  • dongruyan4948 2015-02-12 05:59

    JavaScript allows either single or double quotes for strings, but JSON only allows double quotes. See http://www.json.org/

    See also jQuery.parseJSON single quote vs double quote

    打赏 评论
  • dppxp79175 2015-02-12 06:03

    Just Use

    function getAllAnswersToHitViaAjax($theJobName) {
       $testData[0] = 'testing123';
       echo json_encode($testData);
    }
    

    Then in your AJAX you can do

    $.ajax({
     url: path,
     type: 'GET',
     dataType: 'json'
     success: function(data){
        for (var i = 0; i < data.length; i++) {
            //DO YOUR STUFF          
        }
     }
    });
    
    打赏 评论
  • dongyou7739 2015-02-12 06:21
    Try with the below code,
    
        function getAllAnswersToHitViaAjax($theJobName) {
        $testData[0] = 'testing123';
        echo json_encode($testData);
        die();
        }
    
        $.ajax({
        url: path,
        type: 'GET',
        dataType: 'json'
        success: function(data){
            console.log(data);
            var result = JSON.parse(data);
            console.log(result);
        }
        });
    
    打赏 评论

相关推荐 更多相似问题