Jquery $ .ajax doesn't work in firefox against rails (406 answer) (works in chrome and IE)

I have a rails backend and am testing the following jquery code:

var content = $("#notification_content").val(); var data = new Object(); data.content = content; $.ajax({ url: "/notifications/detect_type.json", type:"POST", data: data, success: function(result ){updateTypeDropDown(result)}}); 

This code works fine in Chrome and IE. However, in Firefox (using Firebug), I see the following: http: // localhost: 3000 / notifications / detect_type.json 406 Not acceptable

here is the firefox request in the log:

Processing NotificationsController # detect_type (for 127.0.0.1 on 2010-12-21 17:05:59) [POST] Parameters: {"Action" => "detect_type", "content" => "226 south emerson denver co 80209", "controller" => "notifications"} User Columns (2.0ms) SHOW FIELDS FROM users User load (37.4 ms) SELECT * FROM users WHERE ( users . id = '1') LIMIT 1 Completed in 58 ms (View: 1, DB: 40) | 406 Not acceptable [http: //localhost/notifications/detect_type.json]


Here is the chrome query in the log:

Processing NotificationsController # detect_type (for 127.0.0.1 on 2010-12-21 17:06:41) [POST] Parameters: {"action" => "detect_type", "content" => "226 south emerson 80209", "controller "=>" notification "}
Custom Columns (2.1ms) SHOW FIELDS FROM users
User load (30.4 ms)
SELECT * FROM users WHERE ( users . id = '1') LIMIT 1 Completed in 100 ms (view: 1, DB: 33) |
200 OK [http: //localhost/notifications/detect_type.json]

I'm at a dead end. Ideas?

+1
source share
2 answers

Oddly enough, the solution was to do this from the rails:

 format.js { render :text => type.to_json } format.json { render :json => type.to_json } 

JQuery error? Not sure...

+2
source

Based on a quick search, it looks like maybe 406 indicates a browser failure (in this case Firefox) to accept the type of content delivered from the server for the request. (This is one such explanation.)

Try setting up Firefox to accept json. Based on this post , it looks like Firefox can use the extension ...

UPDATE

Since this is apparently a direct use of $ .ajax, you should be able to get this to work without any odd changes to client settings in Firefox. Try explicitly tell jquery what the return type should be by adding a parameter during init, for example:

dataType: "json"

See the relevant jquery docs here for more details.

0
source

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


All Articles