简体   繁体   English

无法使用JSONP获取远程JSON

[英]Failing to get remote JSON using JSONP

I'm trying to retrieve a remote JSON using jQuery 1.11.1. 我正在尝试使用jQuery 1.11.1检索远程JSON。 The remote server does support jsonp and I'm able to download the .jsonp file by simply entering the call address and ?callback=foo in a browser. 远程服务器确实支持jsonp,我只需在浏览器中输入呼叫地址和?callback=foo就能下载.jsonp文件。

However, when I try to get it using ajax, it fails. 但是,当我尝试使用Ajax获取它时,它会失败。

$.ajax({
    type: "GET",
    url: "http://path-to-remote-server.net/file.jsonp",
    dataType: 'jsonp',
    jsonp : "callback",
    jsonpCallback: "e",
    success: function(r) {
        console.log(r);
    }
});

A quick look at the console tells me that it's a bad request, probably because it seems that jQuery passes a second unwanted parameter, making the request look like this : 快速浏览控制台可知这是一个错误的请求,可能是因为jQuery似乎传递了第二个不需要的参数,从而使请求看起来像这样:

http://path-to-remote-server.net/file.jsonp?callback=e&_=1406722474006

This happens even when I omit the jsonp and jsonpCallback options. 即使我省略了jsonp和jsonpCallback选项,也会发生这种情况。 The request then looks like this : 然后,请求如下所示:

http://path-to-remote-server.net/file.jsonp?callback=jQuery111106199050471186638_1406722685544&_=1406722685545

Using the short cut $.getJSON doesn't work either, but not for the same reason it seems : 使用快捷方式$ .getJSON也不起作用,但出于同样的原因,它似乎也是如此:

$.getJSON("http://path-to-remote-server.net/file.jsonp?callback=e", function(r){
    console.log(r);
});

This doesn't trigger any error in the console, but nothing gets logged either, as if it didn't get anything back from the server. 这不会在控制台中触发任何错误,但是也不会记录任何内容,就好像它没有从服务器取回任何东西一样。

Why is that, and how can I avoid it? 为什么会这样,我该如何避免呢?

Thank you all in advance! 谢谢大家!

From the manual : 手册

cache (default: true, false for dataType 'script' and 'jsonp') 缓存(默认:true,对于dataType'script'和'jsonp'为false)
Type: Boolean 类型:布尔
If set to false, it will force requested pages not to be cached by the browser. 如果设置为false,将强制浏览器不缓存请求的页面。 Note: Setting cache to false will only work correctly with HEAD and GET requests. 注意:将缓存设置为false只能与HEAD和GET请求一起正常使用。 It works by appending "_={timestamp}" to the GET parameters. 它通过在GET参数后附加“ _ = {timestamp}”来工作。 The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET. 其他类型的请求不需要该参数,但在IE8中,当GET对已经由GET请求的URL进行POST时,则不需要该参数。

So add cache: true to your Ajax parameters. 因此,将cache: true添加到您的Ajax参数。

The last query string argument automatically added by jQuery is needed to avoid browser caching. 需要使用jQuery自动添加的最后一个查询字符串参数,以避免浏览器缓存。 Otherwise if you do the same JSONP call in the same or in any other page of your site, you will get the cached result instead of the fresh one. 否则,如果您在网站的同一页面或任何其他页面中执行相同的JSONP调用,则将获得缓存的结果,而不是最新的结果。 So if your server does support JSONP it should accept such requests. 因此,如果您的服务器确实支持JSONP,则它应接受此类请求。

It seems to be a server-side issue, so you should check what is going on there :) 这似乎是服务器端的问题,因此您应该检查发生了什么事情:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM