很多故障排除之後,我無法弄清楚,爲什麼我的表單的Ajax:成功回調不被稱爲軌道3軌道3 - 阿賈克斯形式AJAX:成功事件不會被調用
以下是代碼
JS代碼:
$(function() {
$("#myform").bind('ajax:success', function(data, status, xhr) {
alert("form success");
console.log(data);
console.log(status);
}).bind('ajax:error', function(xhr, status, error) {
console.log(error);
console.log(status);
});
});
阿賈克斯形式:在我看來,最終產生
<form accept-charset="UTF-8" action="/myc/mya" data-remote="true" id="myform" method="post"> <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="hZuhxwboQj2qo8MPIR8DWwYMqAC1V2yqG+DX9UnlIr0=" /></div>
<input class="large" id="foo" name="foo" type="text" />
<input class="btn success" data-disable-with="Working..." name="commit" type="submit" />
</form>
此之後發生在操作的邏輯,其形式帖到:
@foo = { :foo => "bar" }
render(:json => @foo.to_json, :content_type => 'application/json',
:layout => false)
我已經Firebugged這整個請求/響應:
It says 200 OK. And the Response Headers are:
Cache-Control max-age=0, private, must-revalidate
Connection Keep-Alive
Content-Length 13
Content-Type application/json; charset=utf-8
Date Mon, 23 Jan 2012 10:37:19 GMT
Etag "9bb58f26192e4ba00f01e2e7b136bbd8"
Server WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)
Set-Cookie _session_id=BAh7CkkiD3Nlc3Npb25faWQGOgZFRiIlMGIzZGRhZDQ2ZjNmYzUzOWJkMzBkZWMxZDBmMDgwODZJIhBfY3NyZl90b2tlbgY7AEZJIjFoWnVoeHdib1FqMnFvOE1QSVI4RFd3WU1xQUMxVjJ5cUcrRFg5VW5sSXIwPQY7AEZJIhJyZXF1ZXN0X3Rva2VuBjsARkkiMGlCVmFZYllrU2R1RHl2aVZ4ZGc3ZWxKdmVxZkdFa1VKYWVXaHU0eXBmR1kGOwBUSSIZcmVxdWVzdF90b2tlbl9zZWNyZXQGOwBGSSIwcFdGTUVTUGt6d2VSY1pzV2YxU3JLNmFJSEpCU3FGMDh2ZmZ2U0pFaVI4cwY7AFRJIhRjdXJyZW50X3VzZXJfaWQGOwBGSSIpZDZiZDdhMjYtNDRjNi0xMWUxLTkwMDItZmExMjJjNjJmZTFmBjsARg%3D%3D--78099c697506ede3c7e9a833efa1785c6f1b9a6e; path=/; HttpOnly
X-Runtime 0.354051
X-Ua-Compatible IE=Edge
的響應是,如預期的那樣:
{"foo":"bar"}
200 OK響應應該在我的JS中觸發'ajax:success'...但是這不會發生。有什麼想法嗎?錯誤回調沒有被擊中要麼...
任何在JS控制檯? JS中的缺失paren是一個錯字嗎? –
Nop,在JS控制檯中沒有錯誤..修復paren以防萬一,仍然無法正常工作。如果'ajax:success'事件已綁定到窗體並且它已經在控制檯中進行檢查。 – deruse
更新問題中的代碼,然後;它會讓他們的人離開。沒有「以防萬一」;沒有它JS被打破。 –