0

RailsコントローラーとJSファイルの間で情報をやり取りしています。

  1. JS経由でコントローラーにフォームを送信しています(動作)

    $("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post' });
    
  2. コントローラーでイベントをキャッチできます(動作)

    def send_help_email
      ....
    end
    
  3. 上記のリクエストを送信したのと同じ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();
});
4

1 に答える 1

1

ajaxSubmitドキュメントによると、jQuery.ajaxメソッドと同じオプションを受け入れます。したがって、応答を取得するには、completeコールバックを呼び出しに渡すことができます。

var handleResponse = function(data) {
  // Use response here
};

$("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post', complete: handleResponse });

使用しているjQueryのバージョンによっては、次completeの戻り値のメソッドを介してコールバックを渡すこともできjQuery.ajaxます。

$("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post'}).complete(function(data) {
  // Use response here
});
于 2012-10-07T15:54:03.580 に答える