0

I'm sending information back-and-forth between a Rails controller and a JS file.

  1. I'm sending the form to the controller through JS (works)

    $("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post' });
    
  2. I'm able to catch the event in the controller (works)

    def send_help_email
      ....
    end
    
  3. In the same JS file that sent the above request, how do I capture the JSON response (below)? (doesn't work)

    def send_help_email
      ...
      cst_str = @current_support_ticket.to_s
      respond_to do |format|
        format.json { render :json => cst_str }
      end
    end
    

    In the JS file

    var xmlhttp = new XMLHttpRequest();
    alert(xmlhttp.responseText);
    

UPDATE

I noticed a JS error that is preventing the success: function from executing:

Error

TypeError: 'undefined' is not a function (evaluating '$("#help-email- form").ajaxSubmit({url: '/help/send_help_email', type: 'post', complete: handlerResponse })')

This is the line that is triggering the error

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

This is complete block

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
  • Is there some sort of stack error you can post? Commented Oct 7, 2012 at 15:50
  • What does $.fn.ajaxSubmit on the console give you? Commented Oct 7, 2012 at 17:14
  • $.fn.ajaxSubmit returns undefined Commented Oct 7, 2012 at 17:50
  • @VerdiErelErgün it seems that you don't have the plugin loaded them. Check out if it's included properly on your page with a <script> tag, and if it is after the jQuery <script> tag. Commented Oct 7, 2012 at 19:04

1 Answer 1

1

According to ajaxSubmit documentation, it accepts the same options that the jQuery.ajax method. So, to get the response, you can pass the complete callback to the call:

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

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

Depending on the version of jQuery that you are using, you can also pass the callback through the complete method on the return value from jQuery.ajax:

$("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post'}).complete(function(data) {
  // Use response here
});
Sign up to request clarification or add additional context in comments.

8 Comments

That seems to maybe work, I'm getting a type error now, which I think is preventing the code in handlerResponse from executing (although the post is working just fine) TypeError: 'undefined' is not a function (evaluating '$("#help-email-submit").ajaxSubmit({url: '/help/send_help_email', type: 'post', complete: handlerResponse })')
The name of the function in my code is handleResponse, not handlerResponse. Check out if it matches on your code
Ahhh I noticed that and changed that. I found this information on ajaxSubmit. It looks like it's not part of jQuery core and needs some external libraries. jquery.malsup.com/form
Yes, I thought you were using it already, since you said that you were able to capture the event on your ruby code.
Ah shoot, I uprgraded the plugin and jquery and that still doesn't seem to have solved the problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.