在Firefox中使用Jquery $ .post但在Safari或Chrome中不使用时出现错误 [英] 406 Not Acceptable error when using Jquery $.post in Firefox but not in Safari or Chrome

查看:385
本文介绍了在Firefox中使用Jquery $ .post但在Safari或Chrome中不使用时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jquery(1.3.2)$ .post命令触发ajax调用Rails服务器。



这些代码在Safari和谷歌浏览器(Mac),但当我尝试在Firefox(3.5.7),我有一个奇怪的'406不能接受'的错误。



当我看它表明它只接受text / javascript响应。而Content-Type的响应是text / html在Chrome中,接受的类型是'application / json,text / javascript, / ,text / javascript'和响应内容类型是application / json; charset = utf-8'。



我试图强制内容类型在rails中为'text / javascript'

  format.json do 
render:json => @ races.to_json,:content_type => 'text / javascript'
end

Chrome中的内容类型确实发生了变化,在Firefox中它仍然是'text / html'。

以下是我用来触发ajax调用的代码。




/ locator,
params,
function(data){...},
json
);

有什么我可以做的,使这个工作在Firefox?
谢谢

解决方案

在post调用中在您的URL中添加.json扩展名

  $。post(
/locator.json
...
pre>

或者(可能更好)将以下内容添加到您的application.js中,为所有的ajax请求设置标题

<$ ($ {$ b $'beforeSend':function(xhr){xhr.setRequestHeader(Accept,text / javascript)}
})$ $ {code> b $ b


I'm using Jquery (1.3.2) $.post command to trigger an ajax call to a rails server.

The code works great on Safari and on Google Chrome (mac), but when I tried it on Firefox (3.5.7), I got a weird '406 Not Acceptable' error.

When I look at the headers, it Firefox indicated that it accepted only ' text/javascript' responses. And the response Content-Type was 'text/html; charset=utf-8'.

In Chrome the accepted types were 'application/json, text/javascript, /, text/javascript' and the response Content-Type was 'application/json; charset=utf-8'.

I tried to force the content type in rails to 'text/javascript'

format.json do
   render :json => @races.to_json, :content_type => 'text/javascript'
end

The content type is indeed changed in Chrome, but not in Firefox where it remains 'text/html'.

Here is the code I used to trigger the ajax call.

$.post(
    "/locator",
    params, 
    function(data){...},
    "json"
);

Is there something I can do to make this work in Firefox? Thanks

解决方案

Add a .json extension to your URL in the post call

$.post(
"/locator.json"
...

Or (possibly better) add the following to your application.js to set headers for all ajax requests

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

这篇关于在Firefox中使用Jquery $ .post但在Safari或Chrome中不使用时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆