.ajax always怎么用,$.ajax().always() or $.ajax().complete()

So i'm starting to migrate from the prototype library to jquery.  the ajax commands are pretty similar, but i'm struggling with jquery's callbacks.  I know i either want to use the .complete() or .always().  The .success() and .error() callbacks aren't for me since order of operations is important and i generally want to perform the same set of functions whether or not it was successful.

Basically I use ajax in 2 ways.  1) to send a command to the server and get back a JSON object with the result info. and 2) to pull back plain HTML content and put it in a div.

I know the docs say to start using .always() instead of .complete().  but they function differently.  As far as .complete() goes, it returns the jqXHR object which has jqXHR.responseText.  this is fine for just HTML but I have to manually parse the JSON (unless i'm missing something).  With .always(), it returns a pre-parsed string or object depending on whether it was HTML or JSON, the status, and the jqXHR object.  This is great until the status isn't 'success'.  In that case, the variable that supposedly contained the response from the server now contains the jqXHR object.  It seems like i now have to do a bunch of ifs in order to determine what to do when.  I can't just throw the contents of the response variable in my div because it could be the jqXHR object and i can't treat it as my JSON object because now its an object of a different kind.

Any thoughts to steer me in the right direction?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值