getJSON 调用中的错误处理
如何处理 getJSON 调用中的错误?我尝试使用 jsonp 引用跨域脚本服务,如何注册错误方法?
How can you handle errors in a getJSON call? Im trying to reference a cross-domain script service using jsonp, how do you register an error method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
$.getJSON()
是常规 AJAX 调用的一种抽象,您必须在其中告知您需要 JSON 编码的响应。您可以通过两种方式处理错误:一般方式(通过在实际调用 AJAX 调用之前配置它们)或具体方式(使用方法链)。
“通用”类似于:
“特定”方式:
$.getJSON()
is a kind of abstraction of a regular AJAX call where you would have to tell that you want a JSON encoded response.You can handle errors in two ways: generically (by configuring your AJAX calls before actually calling them) or specifically (with method chain).
'generic' would be something like:
And the 'specific' way:
有人给卢西亚诺这些要点:)
我刚刚测试了他的答案 - 有一个类似的问题 - 并且工作得很好......
我什至加上了我的 50 美分:
Someone give Luciano these points :)
I just tested his answer -had a similar question- and worked perfectly...
I even add my 50 cents:
这是我的补充。
来自 http://www.learnjavascript.co.uk/jq/reference/ajax/getjson.html 和官方来源
“jqXHR.success()、jqXHR.error()、和 jqXHR.complete() 回调
从 jQuery 1.8 开始,jQuery 1.5 中引入的方法已被弃用。到
为最终删除准备代码,使用 jqXHR.done(),
jqXHR.fail() 和 jqXHR.always() 。"
我这样做了,这是 Luciano 更新的代码片段:
并带有错误描述,并将所有 json 数据显示为字符串:
如果您不喜欢警报,用
console.log
替换它们Here's my addition.
From http://www.learnjavascript.co.uk/jq/reference/ajax/getjson.html and the official source
"The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback
methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. To
prepare your code for their eventual removal, use jqXHR.done(),
jqXHR.fail(), and jqXHR.always() instead."
I did that and here is Luciano's updated code snippet:
And with error description plus showing all json data as a string:
If you don't like alerts, substitute them with
console.log
我知道自从有人在这里回答以来已经有一段时间了,发帖者可能已经从这里或其他地方得到了他的答案。不过,我确实认为这篇文章将帮助任何人在执行 getJSON 请求时寻找一种方法来跟踪错误和超时。因此,下面我对问题的回答
getJSON 结构如下(在 http://api.jqueri.com):
大多数人使用
url var 提供 JSON 数据的链接来实现这一点,将 datatosend 作为添加 < 的位置code>"?callback=?" 以及必须发送的其他变量以获得返回的正确 JSON 数据,以及 success 函数作为处理数据的函数。
但是,您可以在成功函数中添加 status 和 xhr 变量。 status 变量包含以下字符串之一:“success”、“notmodified”、“error”、“timeout”或“parsererror”,xhr 变量包含返回的 XMLHttpRequest 对象
(在 w3schools 上找到)
通过这种方式,可以轻松跟踪超时和错误,而无需实现自定义超时请求完成后启动的跟踪器。
希望这对仍在寻找该问题答案的人有所帮助。
I know it's been a while since someone answerd here and the poster probably already got his answer either from here or from somewhere else. I do however think that this post will help anyone looking for a way to keep track of errors and timeouts while doing getJSON requests. Therefore below my answer to the question
The getJSON structure is as follows (found on http://api.jqueri.com):
most people implement that using
where they use the url var to provide a link to the JSON data, the datatosend as a place to add the
"?callback=?"
and other variables that have to be send to get the correct JSON data returned, and the success funcion as a function for processing the data.You can however add the status and xhr variables in your success function. The status variable contains one of the following strings : "success", "notmodified", "error", "timeout", or "parsererror", and the xhr variable contains the returned XMLHttpRequest object
(found on w3schools)
This way it is easy to keep track of timeouts and errors without having to implement a custom timeout tracker that is started once a request is done.
Hope this helps someone still looking for an answer to this question.
它在 jQuery 2.x 中得到修复;在 jQuery 1.x 中你永远不会收到错误回调
It is fixed in jQuery 2.x; In jQuery 1.x you will never get an error callback
为什么不呢
?
有关使用的
.fail()
方法(jQuery 1.5+)的详细信息,请参阅 http://api.jquery.com/jQuery.ajax/#jqXHR链接。
由于
jqXHR
由函数返回,因此可以进行Why not
??
For details on the used
.fail()
method (jQuery 1.5+), see http://api.jquery.com/jQuery.ajax/#jqXHRSince the
jqXHR
is returned by the function, a chaining likeis possible.
我也遇到了同样的问题,但我没有为失败的请求创建回调,而是简单地返回了 json 数据对象的错误。
如果可能的话,这似乎是最简单的解决方案。这是我使用的 Python 代码示例。 (使用 Flask、Flask 的 jsonify f 和 SQLAlchemy)
然后你可以像这样检查 Javascript;
I was faced with this same issue, but rather than creating callbacks for a failed request, I simply returned an error with the json data object.
If possible, this seems like the easiest solution. Here's a sample of the Python code I used. (Using Flask, Flask's jsonify f and SQLAlchemy)
Then you can check on Javascript like this;
在某些情况下,您可能会遇到使用此方法的同步问题。
我在 setTimeout 函数中编写了回调调用,并且它同步工作得很好 =)
EG:
In some cases, you may run into a problem of synchronization with this method.
I wrote the callback call inside a
setTimeout
function, and it worked synchronously just fine =)E.G:
这是一个相当古老的线程,但它确实出现在 Google 搜索中,所以我想我会使用 Promise 添加 jQuery 3 答案。此代码片段还显示:
代码片段是:
This is quite an old thread, but it does come up in Google search, so I thought I would add a jQuery 3 answer using promises. This snippet also shows:
The code snippet is: