JavaScript AJAX&;轨道控制器(前后)
我在Rails控制器和JS文件之间来回发送信息JavaScript AJAX&;轨道控制器(前后),javascript,ruby-on-rails,ruby,ruby-on-rails-3,jquery-events,Javascript,Ruby On Rails,Ruby,Ruby On Rails 3,Jquery Events,我在Rails控制器和JS文件之间来回发送信息 我通过JS(works)将表单发送给控制器 我能够在控制器(works)中捕获事件 在发送上述请求的同一个JS文件中,如何捕获JSON响应(如下所示)?(不起作用) 在JS文件中 var xmlhttp = new XMLHttpRequest(); alert(xmlhttp.responseText); 更新 我注意到一个JS错误正在阻止success:函数执行: 错误 TypeError:“undefined”不是函数(计算“$”(
var xmlhttp = new XMLHttpRequest();
alert(xmlhttp.responseText);
更新 我注意到一个JS错误正在阻止success:函数执行: 错误 TypeError:“undefined”不是函数(计算“$”(“#帮助电子邮件-表单”)。ajaxSubmit({url:”/help/send_帮助电子邮件,键入:“post”,完成:handlerResponse}) 这是触发错误的行
$("#help-email-form").ajaxSubmit({url: '/help/send_help_email', type: 'post', complete: handlerResponse })
这是一个完整的街区
var handlerResponse = function(data) {
alert(data);
};
$('#help-email-submit').live('click', function(e) {
$('#sender-email-wrapper').fadeOut('fast', function() {
$("#help-email-sent").fadeIn('slow');
});
$("#help-email-form").ajaxSubmit({url: '/help/send_help_email', type: 'post', complete: handlerResponse })
e.preventDefault();
});
根据,它接受的选项与。因此,要获得响应,您可以将complete
回调传递给调用:
var handleResponse=函数(数据){
//在这里使用响应
};
$(“#帮助电子邮件提交”).ajaxSubmit({url:'/help/send#help#email',键入:'post',完成:handleResponse});
根据您使用的jQuery版本,还可以通过complete
方法对jQuery.ajax的返回值传递回调:
$("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post'}).complete(function(data) {
// Use response here
});
您是否可以发布某种堆栈错误?控制台上的$.fn.ajaxSubmit
为您提供了什么信息?$.fn.ajaxSubmit
返回undefined@VerdiErelErgün看起来你没有加载插件。检查页面上是否正确包含了
标记,以及它是否位于jQuery
tag.Doh之后。这修复了它,多亏了Renato。这似乎可行,我现在遇到了一个类型错误,我认为它阻止了handlerResponse中的代码执行(尽管帖子运行得很好)TypeError:“undefined”不是一个函数(计算“$”(“#帮助电子邮件提交”)。ajaxSubmit({url:'/help/send_help_email',键入:'post',完成:handleresponse}'))我的代码中的函数名是handlerResponse
,而不是handlerResponse
。检查它是否与您的代码匹配。我注意到了这一点,并对其进行了更改。我在ajaxSubmit上找到了此信息。它看起来不是jQuery核心的一部分,需要一些外部库。是的,我以为您已经在使用它了,因为您我知道你能够在你的ruby代码中捕获事件。啊,天哪,我升级了插件和jquery,但这似乎仍然没有解决问题。
$("#help-email-form").ajaxSubmit({url: '/help/send_help_email', type: 'post', complete: handlerResponse })
var handlerResponse = function(data) {
alert(data);
};
$('#help-email-submit').live('click', function(e) {
$('#sender-email-wrapper').fadeOut('fast', function() {
$("#help-email-sent").fadeIn('slow');
});
$("#help-email-form").ajaxSubmit({url: '/help/send_help_email', type: 'post', complete: handlerResponse })
e.preventDefault();
});
$("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post'}).complete(function(data) {
// Use response here
});