JQuery Form Plugin + Ajax + Rails File Download

I am using jQuery Form Plugin in big ass form, which includes some file fields. The back is Ruby on Rails.

I'm having trouble getting Rails to recognize the type of POST request as "text / javascript", although I believe that I am installing it correctly.

The form looks like this:

<form id="listing_form" method="POST" enctype="multipart/form-data" action="/listings">
    <input type="text" size="30" name="listing[venue][phone]" id="listing_venue_phone"/>
    <input type="file" size="30" name="listing[venue][image]" id="listing_venue_image"/>
    ...plus tons of other fields

js looks like this:

$("#listing_form").ajaxForm({ dataType: 'script' });

And I also stated:

jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) {xhr.setRequestHeader('Accept', 'text/javascript')}
});

The request is sent to the controller, but the Rails reply_to block sees it as a regular html request. In addition, Firebug shows an analyzer error and does not display request and response headers in the console.

If I delete the file field and POST only text fields, the response_to block processes the request as a js request and properly executes the create.js.erb file.

Rails ? .

+3
2

- JavaScript () XHR. "" ajax iframe. , jQuery Form.

Rails "" ajax, XHR jQuery Form beforeSubmit:

// ...
beforeSubmit: function(data) {
    data.push({name: 'accept_js', value: '1'});
}
// ...

before, :js, :

before_filter :set_ajax_request

# ...

private
  def set_ajax_request
    request.format = :js if params[:accept_js] == '1'
  end

respond_to .

+4

Source: https://habr.com/ru/post/1722964/


All Articles