【问题标题】:jQuery Ajax callback was not called. ParserError未调用 jQuery Ajax 回调。解析器错误
【发布时间】:2013-12-09 12:27:54
【问题描述】:

我有一个调用 jquery ajax 的函数来访问用 C# 开发的宁静服务。代码如下

function refreshUsage(macAdd)
{
    alert(macAdd);
    ajax = $.ajax({ 
       type: "GET",    
       data: {'mac': '1'},
       dataType: "jsonp",
       jsonp:false,
       async:false,
       jsonpCallback:'blah',
       crossDomain: true,
       cache: false,
       contentType: "application/json; charset=utf-8",
       url: "http://localhost:20809/api/keyGen",      
       ajaxSuccess: function(data)
       {
        data = JSON.parse(data);
        console.log("data _" +data);
       },
       error: function(xhr, status, error) {
        if (status === 'parsererror') {
                console.log("resptext__" + xhr.responseText)
            }
          console.log("status _" +status);
          console.log("error _" +error);
       },     
       complete: function(response)
       {
        console.log("response _" +response);
       },

    });  
}
var blah = function (data) {
    alert(data);
    //do some stuff
}

当我点击这个给定的 url 时,它会在浏览器窗口中发送响应。但是当我试图在 ajax 函数中获取响应文本时,即使成功代码是 200 并且成功文本是成功的,它也是未定义的。我收到以下错误但没有响应:

resptext__undefined
状态 _parsererror
错误_Error:没有调用blah

【问题讨论】:

  • 您没有从服务器返回有效的 JSONP。
  • 您能粘贴一个您从服务器返回的 json 示例吗?最有可能那里发生了一些无效的事情......
  • 另外,你到底在做什么?您已经添加了几乎所有选项,并且您希望返回 JSONP,但是您已将回调键名称(jsonp 选项)设置为 false,转为异步,这在执行 异步 Javascript和Xml,并添加了自己的回调函数,基本上去掉了成功处理程序,并且在做JSONP请求时没有错误处理程序。
  • ajaxSuccess 还是成功??
  • JSONP 并不是真正的 ajax,它是一个插入 DOM 的脚本标签。重要的是返回的代码是封装在函数中的有效 JSON,因此 JSONP 将始终看起来像 function_name( {json:here} ),并且您的代码没有封装函数,因此它不是有效的 JSON,并且您会收到解析错误

标签: javascript ajax jquery


【解决方案1】:

这是一个例子。试试这个:

<html>
        <head>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
            <script>
                $(document).ready(function(){
                    $.ajax({
                        url: 'http://twitter.com/status/user_timeline/padraicb.json?count=10',
                        dataType: 'jsonp',
                        success: function(dataWeGotViaJsonp){
                            var text = '';
                            var len = dataWeGotViaJsonp.length;
                            for(var i=0;i<len;i++){
                                twitterEntry = dataWeGotViaJsonp[i];
                                text += '<p><img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>'
                            }
                            $('#twitterFeed').html(text);
                        }
                    });
                })
            </script>
        </head>
        <body>
            <div id = 'twitterFeed'></div>
        </body>
    </html>
ThatGuy

示例将为您解释。

【讨论】:

  • 看不出有什么不同!
  • 这将为您解释。 Link
【解决方案2】:

对我来说,我必须确保服务器按照 jsonp 回调的预期响应,我必须编辑来自我的 php 服务器的响应,如下所示

    $data = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); 
    echo $_GET['callback'] . '('.json_encode($data).')';

如果您的服务器没有以如下格式回复

             dataWeGotViaJsonp({"statusCode":201}) 

你会得到那个错误

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2012-07-31
    • 1970-01-01
    • 2017-07-31
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多