不带 JSONP 的 getJSON 远程请求 - 服务器

发布于 2024-10-03 20:30:23 字数 196 浏览 0 评论 0原文

我试图从远程服务器调用 JSON 数据,但服务器不返回有效的 JSONP 格式的数据,仅返回有效的 JSON 数据。 (通过验证器确认)

如果我发出 getJSON 请求(使用 &callback=? ),我会得到返回的有效 JSON,但它不会触发回调函数,因为它不是有效的 JSONP。

有什么好的方法可以访问返回的 JSON 数据吗?

I am trying to call JSON-data from a remote Server, but the server doesnt return valid JSONP formed data, only valid JSON data. ( confirmed with validators )

If i make the getJSON request( with &callback=? ), i get the valid JSON returned, but it doesnt trigger the callback function because it isnt valid JSONP.

Is there any good way to get access to the JSON data that was returned?

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

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

发布评论

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

评论(3

天赋异禀 2024-10-10 20:30:24

首先,如果您获取 JSON 的服务器不支持 JSONP,那么您将必须使用代理。如果它确实支持 JSONP,您应该将请求格式化为类似于下面的示例。当您没有在 $.getJSON(...&callback=?) 中指定回调时,您的请求将如下所示:

http://someurl?callback=123489234982

末尾带有一些可笑的数字,这会让您的生活变得痛苦。因此,您应该指定一个回调并格式化您的代码,如下所示:

<script>
     $.getJSON("your url?callback=callbackName", ....);

     function callbackName { do what you want with the json in here }
</script>

如果这不起作用,那么您的服务器不支持 JSONP :( 这里是您可以使用的 php 代理的链接,该代理非常好并且有很多文档

http://benalman.com/projects/php-simple-proxy/

Well first of all if your server your getting the JSON from doesn't support JSONP then your going to have to use a proxy. If it does support JSONP you should format your request a little like the sample below. When you don't specify a callback in the $.getJSON(...&callback=?) then your request looks like this:

http://someurl?callback=123489234982

with some ridiculous number on the end of it and it makes your life a pain. So, you should specify a callback and format your code like the following:

<script>
     $.getJSON("your url?callback=callbackName", ....);

     function callbackName { do what you want with the json in here }
</script>

if that doesn't work then your server doesn't support JSONP :( here is a link to a php proxy you could use that is pretty good and has lots of documentation.

http://benalman.com/projects/php-simple-proxy/

如歌彻婉言 2024-10-10 20:30:23

不,它必须是 JSONP 数据,因为整个事情是如何工作的,它基本上通过创建

换句话说:如果我们可以从远程服务器获取 JSON,为什么 JSONP 会存在? :)

Nope, it has to be JSONP data because of how the whole thing works, it's basically including a JavaScript file by creating a <script> tag...and that response has to be valid JavaScript, an object literal (by itself) isn't valid JavaScript.

Think of it another way: if we could get JSON from a remote server, why would JSONP exist? :)

始终不够 2024-10-10 20:30:23

是的,这很糟糕。我对 Viddler API(浏览器端)也有同样的问题。它正在发送回 JSON 数据,但不是在脚本化 JSON-P 接口中。

因此,您有两个选择:

  1. 正如人们所说,使用后端代理在服务器端发出请求。
  2. 如果您只需要支持一种浏览器(在我的例子中是 Chromium),请使用“chromium-browser --disable-web-security”之类的内容来禁用“same-origin-policy”规则。这将允许您发出跨域请求,但仅限于这种非常特殊的情况。

祝你好运!

Yea, it sucks. I'm having the same issue with the Viddler API (browser side). It's sending JSON data back, but not in scripted JSON-P interface.

Thus, you have two options:

  1. As people have said, use a back-end proxy to make requests server side.
  2. If you only need to support one browser (Chromium in my case), use something like "chromium-browser --disable-web-security" to disable the "same-origin-policy" rule. This will allow you to make cross-domain requests, but only for this very specific situation.

Good luck man!

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