2

I use rails and haml. I send data via Ajax post to a controller in rails:

$.ajax({
type: 'POST',
url: '/preview-image-upload',
processData: false,
contentType: false,
dataType : 'json',
data: data
});

Then I execute the data and render js in the controller:

respond_to do |format|
 if @previewupload.save
  format.js
 end

The object is saved correctly. Also the .js.erb file is correctly rendered according to the server log:

Rendered path/preview_image_upload.js.erb (0.2ms)
Completed 200 OK in 603ms (Views: 8.1ms | ActiveRecord: 8.5ms)

For testing reasons, I put an alert into the preview_image_upload.js.erb file:

alert("Hello World");

but nothing happens. It looks like the .js.erb file is rendered but not executed.

Tried the solution from here: js.erb not executing javascript but is processed rails

{ render layout: false, content_type: 'text/javascript' }

But that did not work.

4
  • Check net tab in google/chrome or firefox for response from server. Commented Jul 3, 2015 at 12:47
  • The response visible in FF Developer is "alert("Hello World");" - no errors are raised. Commented Jul 3, 2015 at 13:34
  • 1
    can you show the full code of a controller action, views ? Commented Jul 3, 2015 at 13:36
  • It may be that the js error is silent Commented May 14, 2018 at 23:38

2 Answers 2

3

The datatype to execute a Rails javascript response from a jQuery AJAX call is script

Sign up to request clarification or add additional context in comments.

Comments

0

I found the solution. I simply have to remove the dataType attribute in the Ajax request. Makes it look like this:

$.ajax({
type: 'POST',
url: '/preview-image-upload',
processData: false,
contentType: false,
data: data
});

Comments

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.