JQuery 的 getJSON() 未正确设置 Accept 标头?

发布于 2024-09-24 12:50:06 字数 917 浏览 3 评论 0原文

看起来人们过去曾遇到过接受标头的问题,但我不确定我的问题是否相关。使用 jQuery 1.4.2,我在使用 getJSON() 获取 JSON 时遇到问题。我可以在 Firebug 中观察请求/响应,看起来问题的根源在于相关资源根据 Accept 标头返回不同的结果。尽管文档说应该设置它,但在 Firebug 中它显示为“/”——显然,我想要“application/json”。这是一个已知的错误吗?我是否应该设置一些我不知道的标志?

ETA:请求是跨站点的(如果这很重要),但我传递了一个 callback=? 查询参数,因此 JQuery (成功!)将其视为 JSONP。我在这种特殊情况下调用的服务支持接受覆盖查询参数(&accept=application/json),所以我让它手动工作,但我仍然认为标头搞砸了很奇怪,我希望我能够修复它,这样我在处理可能不那么宽容的不同服务时就不会再遇到这种情况了。我没有一种简单的方法来从我的开发环境中复制/粘贴代码,但要点如下:

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

如您所见,这并不完全复杂,并且应该(我 99% 确定) ...) 导致发送带有 application/json Accept 标头的 XHR。正如我所说,根据 Firebug 的 Net 控制台,这种情况并没有发生。如果重要的话,这是在 Firefox 3.6.8 中。

预计到达时间:对于仍在阅读本文的任何人来说,是的,这种情况仍在发生,不,我不知道为什么。就像我说的,简单的 getJSON() 调用,非常基本的语法,跨站点,被视为 JSONP,因为它包含回调查询参数。仍然欢迎建议!

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: 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);
}

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生活了然无味 2024-10-01 12:50:06

这不是一个错误。

由于您的调用是跨域的,因此您的浏览器将不允许您进行 XHR 调用(同源策略)。在内部,jQuery 正在使用“

jQuery 通过向您隐藏这些详细信息来发挥其魔力,但不幸的是,在这种情况下,您似乎受到 泄漏抽象定律

This is not a bug.

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 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.

溺孤伤于心 2024-10-01 12:50:06

在没有看到您的代码(这可能会向我们指出一个明显的解决方案)的情况下,您可以尝试使用标准 Ajax 函数并看看是否会得到不同的结果吗?

$.ajax({
  url: '/what.eva',
  dataType: 'json',
  data: '{}',
  success: callbackFunc
});

function callbackFunc(result) {
   alert(result);
} 

Without seeing your code (which might point us to an obvious solution,) can you try using the standard Ajax function and see if you get different results?

$.ajax({
  url: '/what.eva',
  dataType: 'json',
  data: '{}',
  success: callbackFunc
});

function callbackFunc(result) {
   alert(result);
} 
世界如花海般美丽 2024-10-01 12:50:06

这是一个 bug,已在 jquery 网站上关闭。

http://dev.jquery.it/ticket/6551

似乎没有修复为此。

This is a bug which has been closed on the jquery website.

http://dev.jquery.it/ticket/6551

There does not appear to be a fix for this yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文