Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ROR return JSON with 406 Not Acceptable error

When we return JSON output using render :json =>@profiles, the output will return the required results with a 406 error. How can avoid that '406 Not Acceptable' error ?

like image 468
Kris Avatar asked Dec 09 '11 12:12

Kris


Video Answer


1 Answers

I'm more than sure that you have this problem.

Explanations:

Say your controller only returns json answers

def action
  # call
  respond_to do |format|
    format.json { render json: results }
  end
end

This will return the json as soon as:

  • /path_to_action.json is called
  • /path_to_action is called with headers Content-Type:application/json; and probably some other header types (Eg. X-Requested-With:XMLHttpRequest)

Otherwise, it returns a 406 Not Acceptable error.

To avoid the issue, if your controller only returns json, write:

def action
  # call
  render json: results
end

otherwise, use /path_to_action.json instead.

like image 71
Mikhail Nikalyukin Avatar answered Oct 27 '22 10:10

Mikhail Nikalyukin