开发者

Can I call error: function() on specific condition met inside of success: function(data)?

With the following jQuery.ajax response...

{"ysearchresponse": 
{"responsecode":"200","totalhits":"0","deephits":"0","count":"0","start":"0"}}

How d开发者_StackOverflow中文版o I do a comparison on "totalhits" in order to execute the error: function() when totalhits == "0" ?


You can't. (see first comment). And trust me, you don't want to. The error callback is meant for errors related to the ajax request and not for errors only you consider errors (i.e. nonsense data which is valid json/xml/whatever or an empty resultset).

However, if you want your error function called, create it as a regular (named) function instead of an anonymous one and call it manually when your criteria are met. Note that making your function a regular one results in it not being a closure anymore - but without seeing your full $.ajax(...); call and some surrounding code it's impossible to tell if that'll be a problem or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜