Radhika
Radhika

Reputation: 2561

406 Not Acceptable error while making an ajax call

I have the below code snippet in one of my .js.coffee files:

$.ajax({
      url: '/attachinary_files/' + id
      method: 'put'
      }).done(//do something)

In my browser console I get the error:

406 Not Acceptable

I am unable to find the reason. I have required the necessary files in my application.js file, like:

//= require jquery
//= require jquery_ujs
//= require_tree .

I am unable to find what I am missing. Any help would be much appreciated. Thanks:)

Edit:

controller

def update
    super
    attachinary_file = AttachinaryFile.find(params[:id])
    AttachinaryFile.where(attachinariable_id: attachinary_file.attachinariable_id)
      .update_all(is_primary: false)
    attachinary_file.update_column(:is_primary, true)
    respond_to do |format|
      flash.now[:notice] = nil
    end
  end

Upvotes: 2

Views: 4977

Answers (3)

Abbin Varghese
Abbin Varghese

Reputation: 2794

I was using Spring 3.2.5 and got this error. Resolved by using 3.1.1

406 Not Acceptable: Spring 3.2 + JSON + AJAX

Upvotes: 0

joel1di1
joel1di1

Reputation: 773

1) Verify that your controller accept js calls

2) In your JS code, specify that you want a JS formatted result, with headers or with the url :

url: '/attachinary_files/' + id + '.js'

3) If it's not enough, in your controller, specify what you return for JS :

respond_to do |format|
  flash.now[:notice] = nil
  format.js { render js: "alert('Hello Rails');" }
end

Upvotes: 1

vee
vee

Reputation: 38645

You are missing the content type which is the reason for HTTP 406 error.

Update your ajax code to include data-type of the response:

$.ajax({
  url: '/attachinary_files/' + id
  method: 'put'
  dataType: 'script'
}).done(//do something)

Please reference Data-Type (with jQuery) for further details.

Upvotes: 2

Related Questions