gpt4 book ai didi

jquery - Rails 3 - ajax 形式 ajax :success event not being called

转载 作者:行者123 更新时间:2023-12-03 22:30:45 24 4
gpt4 key购买 nike

经过大量故障排除后,我无法弄清楚为什么我的表单的 ajax:success 回调在 Rails 3 中没有被调用。

以下是我认为最终生成的代码:

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);
});
});

Ajax 表单:

<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="&#x2713;" /><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)

我已经对整个请求/响应进行了 Firebug 处理:

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”...但这并没有发生。有什么想法吗?错误回调也没有被命中...

最佳答案

我也遇到了同样的问题。经过 90 分钟的搜索,我终于通过使用以下命令使其工作:

$(document).on('ajax:success', '.delete', function(e) {
$(e.currentTarget).closest('div').fadeOut();
});

致谢:https://stackoverflow.com/users/1609186/ivan-fong

我正在创建然后尝试删除我刚刚创建的内容,所有这些都是通过 ajax 请求进行的。希望这可以帮助别人。

关于jquery - Rails 3 - ajax 形式 ajax :success event not being called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com