Possible Duplicate:
jquery doesn’t call success method on $.ajax for rails standard REST DELETE answer
我响应远程链接(data-remote="true"data-type="json")并输出
format.json { head :ok }
在我的 Rails (3.2.6) Controller 中,它创建了这个 header :
Status Code:200 OK
<小时/>
...
Connection:keep-alive
Content-Length:1
Content-Type:application/json; charset=utf-8
Server:thin 1.4.1 codename Chromeo
Set-Cookie: ... path=/; HttpOnly
X-UA-Compatible:IE=Edge
...
在我的 JavaScript 文件中,ajax:complete 被触发并输出 200 (data.status)。
$( '#myElement' ).on( 'ajax:complete', function( e, data ) {
console.log( data.status );
});
数据如下所示:
...
readyState: 4
responseText: " "
setRequestHeader: function ( name, value ) {...
state: function () {...
status: 200
statusCode: function ( map ) {...
statusText: "OK"
...
对我来说看起来不错...
问题
jQuery (jquery-ujs) 执行 ajax:error 而不是 ajax:success,我不知道为什么,因为没有错误给定。
我研究了很多讨论,但这种方式似乎总是解决方案,而不是问题。感谢您的帮助!
最佳答案
已回复 here .
jQuery 正在等待 JSON 响应。 “head :ok”的响应主体有一个空格,因此 jQuery 无法解析 JSON 响应,因此它仍然认为 200 状态代码是一个错误。
您可以让 Rails 响应 head :no_content
它有一个空主体或 render :json=>true
Rails here 中对此进行了讨论.
head :ok
中出现单个空格的原因Rails 中的解决方案是旧 Safari 错误的解决方法。
关于ruby-on-rails-3 - 导轨 : "head :ok" interpreted as "ajax:error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12407328/