ajax完成回调没有响应201

ajax完成回调没有响应201

本文介绍了Jquery ajax完成回调没有响应201的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ajax帖子:

I have an ajax post as such:

$.post("/api/v1/payment_methods/create_credit_card", values)
.done (response) ->
  console.log("GOOD JOB")
.fail (response) ->
  console.log("Adas")

响应是201,但是,完成了似乎没有抓住它,相反它会失败。我认为201会被认为是成功的,并且会被完成捕获。关于它为什么不起作用的任何想法?

The response is a 201, however, done doesn't seem to be capturing it and instead it's going to fail. I thought 201 would of been considered a success and would of been captured by done. Any ideas of why it wouldn't be working?

注意:上面的代码是在coffeescript中,它并没有真正影响问题,但解释了我的语法

Note: The above code is in coffeescript, which doesn't really affect the question but explains my syntax

推荐答案

所以我们弄清楚出了什么问题,JSON.parse抛出了一个语法错误 - 所以发送的值不是有效的JSON格式。海报无法在chrome中看到语法错误,但是firebug显示错误。

So we figured out what was wrong, JSON.parse was throwing a Syntax Error - so the values sent in isnt in a valid JSON format. The poster wasnt able to see the Syntax error in chrome, but firebug showed the error.

这表明只要Javascript抛出异常,响应可能仍然是200, 201,202等 - 但由于语法错误,会触发fail()函数。

This indicates that whenever Javascript will throw an exception, the response might still be 200, 201, 202 etc. - but because of the syntax error the fail() function would be triggered.

EDIT - 你也应该使用一个不同的响应,许多人使用200 - OK,但Id建议在这种情况下使用202 - ACCEPTED。

EDIT - Also you should probably use a different response, many use 200 - OK, but Id recommend to use 202 - ACCEPTED in this case.

这篇关于Jquery ajax完成回调没有响应201的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:23