RailsコントローラーとJSファイルの間で情報をやり取りしています。
JS経由でコントローラーにフォームを送信しています(動作)
$("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post' });
コントローラーでイベントをキャッチできます(動作)
def send_help_email .... end
上記のリクエストを送信したのと同じJSファイルで、JSON応答(下記)をキャプチャするにはどうすればよいですか?(動作しません)
def send_help_email ... cst_str = @current_support_ticket.to_s respond_to do |format| format.json { render :json => cst_str } end end
JSファイル内
var xmlhttp = new XMLHttpRequest(); alert(xmlhttp.responseText);
アップデート
成功を妨げているJSエラーに気づきました:関数の実行:
エラー
TypeError:'undefined'は関数ではありません('$( "#help-email-form")。ajaxSubmit({url:' / help / send_help_email'、type:' post'、complete: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();
});