【问题标题】:How to retrieve error message from xOptions in jquery-jsonp library如何从 jquery-jsonp 库中的 xOptions 检索错误消息
【发布时间】:2012-06-17 16:52:15
【问题描述】:

我正在使用 jquery-jsonp 库的 2.3.1.min 版本,在此处找到:https://github.com/jaubourg/jquery-jsonp,它按预期工作,即错误发生时触发错误函数。但是,我似乎无法显示遇到的错误。我检查了 github 项目上的文档,但找不到答案。

这是一个限制吗?还是我没有调用正确的对象?

我的实现..

下面的url参数被设置为故意返回404页面。 Chrome 开发工具显示 404 响应,但我似乎无法捕捉到该结果..

    <script type="text/javascript">
        $.jsonp({
                url: 'http://apps.mydomain.com/Service/NonExistant?&max=4&format=json',
                callbackParameter: "callback",
                error: function(xOptions, textStatus){ 
                    // this lines returns "error"
                    console.log(textStatus); 

                    // this returns the Object (but expanding it reveals no indication of error code / message)
                    console.log(xOptions);
                },
                success: function(json, textStatus) {
                    Populate(json); // this works fine
                }
            });
    </script>

【问题讨论】:

    标签: json jquery jsonp


    【解决方案1】:

    很遗憾,你不能。 Johnny Wey 比我解释得好得多 - 但本质上,它依赖于“脚本”标签来获取 JSON 响应,而不是通过 javascript 执行的请求。

    您可以根据 Parsing JSONP Response in Javascript when 4xx or 5xx Http Error Code is Present 在 JSONP 响应中封装错误类型,但这仍然无法帮助您解决 404 错误。

    不幸的是,尽管 JSONP 错误处理实际上并不存在。 IBM 的“注意事项”特别提到“首先,JSONP 调用没有错误处理”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2019-11-21
      相关资源
      最近更新 更多