jQuery的的getJSON()不设置正确接受头? [英] JQuery's getJSON() not setting Accept header correctly?

查看:1486
本文介绍了jQuery的的getJSON()不设置正确接受头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像人曾与过去接受头的问题,但我不知道我的问题是有关的。使用jQuery 1.4.2,我遇到了麻烦JSON与的getJSON()。我可以看的请求/响应中的萤火虫,它看起来像这个问题的来源是有问题的资源返回根据接受头不同的结果。即使文档说应该设置,在Firebug它显示为的 / 的 - 很明显,我想应用/ JSON的。这是一个已知的bug?我应该要设置一些标志,我不知道呢?

It looks like people have had issues with Accept headers in the past, but I'm not sure my issue is related. Using jQuery 1.4.2, I'm having trouble getting JSON with getJSON(). I can watch the request / response in Firebug and it looks like the source of the problem is that the resource in question returns different results depending on the Accept header. Even though the docs say it should be set, in Firebug it shows up as "/" -- obviously, I want "application/json". Is this a known bug? Am I supposed to be setting some flag I'm not aware of?

ETA:请求是跨站点,如果该事项,但我传递一个回调= 查询参数所以jQuery是把它当作?(成功!) JSONP。我打电话在这个特殊的情况下,该服务支持的接受超越查询参数(&放大器;接受=应用程序/ JSON ),所以我得到它的手工工作,但我还是考虑头screwup是陌生的,并希望我能解决这个问题,所以当使用不同的服务,可能不那么宽容处理,我不遇到这个了。我没有一个简单的方法来复制/从我的开发环境贴上code,但这里的要点是:

ETA: The request is cross-site, if that matters, but I'm passing a callback=? query parameter so JQuery is (successfully!) treating it as JSONP. The service I'm calling in this particular case supports an accept override query parameter (&accept=application/json), so I got it to work manually, but I still consider the header screwup to be strange and was hoping I'd be able to fix it, so I don't run into this again when dealing with a different service that might not be so forgiving. I don't have an easy way to copy/paste the code from my development environment but here's the gist:

$.getJSON(baseURL + "?item=" + itemNum + "&callback=?", function(data){
  console.log(data);
}

正如你所看到的,这是不完全复杂,而的(我99%肯定...)导致XHR发送与一个接受头应用程序/ JSON 。就像我说的,这没有发生,每Firebug的网络控制台。如果它很重要,这是在Firefox 3.6.8。

As you can see, this is not exactly complex, and should (I'm 99% sure...) result in an XHR being sent with an Accept header of application/json. Like I said, that's not happening, per Firebug's Net console. If it matters, this is in Firefox 3.6.8.

ETA还是那句话:任何人还在读这些文字,是的,它仍然发生,没有,我不知道为什么。就像我说的,简单的getJSON()调用,真正的基本语法,跨站点,视为JSONP,因为它包括一个回调查询参数。给点建议仍然打开!

ETA Again: For anybody still reading this, yes, it's still happening, and no, I have no idea why. Like I said, simple getJSON() call, really basic syntax, cross site, treated as JSONP because it includes a callback query parameter. Still open to suggestions!

推荐答案

这是不是一个错误。

由于您的通话跨域,您的浏览器将不会让你做出XHR调用(同源策略)。在内部,jQuery是围绕这个工作用了&LT;脚本&GT; 标记黑客,使跨域调用(这是<$ C $背后的基波想法C> JSONP 数据类型)。由于呼叫是使用标签制作,这是根本不可能的jQuery修改头的接受部分。

Since your call is cross-domain, your browser will not allow you to make XHR calls (same-origin policy). Internally, jQuery is working around this using the "<script> tag hack", to make the cross-domain call (this is the fundemental idea behind the JSONP data type). Since the call is made using the tag, it is simply not possible for jQuery to modify the accepts portion of the header.

jQuery的工作它的魔力隐藏,从你这些细节,但不幸的是在这种情况下,你似乎是受法漏抽象的。

jQuery works its magic by hiding these details from you, but unfortunately in this case you seem to be subject to the Law of Leaky Abstractions.

这篇关于jQuery的的getJSON()不设置正确接受头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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