Javascript JSONP 在 Chrome 中工作但在 Firefox/IE 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27462773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSONP working in Chrome but not in Firefox/IE?
提问by Daniel
I'm developing a mobile site and I'm using JSONP requests via jQuery to contact the data server to retrieve info for displaying on the mobile site. I was told not to use a PHP script as a proxy since it would cause extra unnecessary load on the mobile server (millions of users) and to strictly do this client-side. I'm using the following code:
我正在开发一个移动站点,我通过 jQuery 使用 JSONP 请求来联系数据服务器以检索信息以在移动站点上显示。我被告知不要使用 PHP 脚本作为代理,因为它会在移动服务器(数百万用户)上造成额外的不必要的负载,并严格执行此客户端。我正在使用以下代码:
var get_vars = '&callback=?&var=here';
$.ajax({
url: "http://server.com/script?" + get_vars,
type: "GET",
dataType: 'jsonp',
//xhrFields: { withCredentials: true },
//crossDomain: true,
success: function(data){
console.log(data)
}
});
The server uses cookie authentication to determine if the user is logged in before returning data. Strangely enough, this code worked once on Firefox. Subsequent reloads/refreshes of the page resulted in the credentials not being verified by the server. At first I thought it was due to some changes to my code but after testing it in Google Chrome, it works 100% of the time. There are no JS errors displayed in the console for Firefox/IE either. I made sure this wasn't a caching issue and also tried this on another machine with Firefox, to no avail. This issue also happens on Windows Phone and the latest version of Internet Explorer on Windows 8. I'm assuming it must be cookie-related and somehow the credentials aren't being passed to the remote server.
服务器在返回数据之前使用 cookie 身份验证来确定用户是否已登录。奇怪的是,这段代码在 Firefox 上运行了一次。页面的后续重新加载/刷新导致服务器未验证凭据。起初我认为这是由于对我的代码进行了一些更改,但是在 Google Chrome 中对其进行了测试后,它 100% 的时间都可以正常工作。Firefox/IE 的控制台中也没有显示 JS 错误。我确定这不是缓存问题,并且还在另一台装有 Firefox 的机器上尝试了这个,但无济于事。此问题也发生在 Windows Phone 和 Windows 8 上最新版本的 Internet Explorer 上。我假设它必须与 cookie 相关,并且凭据没有以某种方式传递到远程服务器。
As for trying to use CORS... I tried it (as you can see the commented out bits, plus I tried adding $.support.cors = true), and couldn't get it to work. I kept getting the "cross domain unauthorized" error, despite having the server send out the following headers:
至于尝试使用 CORS ......我试过了(你可以看到注释掉的位,加上我尝试添加 $.support.cors = true),但无法让它工作。尽管服务器发送了以下标头,但我仍然收到“跨域未授权”错误:
Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: *
Anyone have any clue what could be causing this? I'd like to get this working with JSONP since it is working in Chrome already.
任何人都知道可能导致这种情况的原因是什么?我想让它与 JSONP 一起工作,因为它已经在 Chrome 中工作了。
Extra notes: It appears another developer isn't having the same issues as me. He is reporting that it's working in Firefox 100% of the time. I even tried the following:
额外说明:看起来另一个开发人员没有和我一样的问题。他报告说它 100% 的时间都在 Firefox 中工作。我什至尝试了以下方法:
- Loading up Firefox in a Virtual Machine and ran into the same problems (to ensure not an OS issue)
- Cleared cache on my phone, disabled wifi, and connected via a separate IP with same issue
- 在虚拟机中加载 Firefox 并遇到相同的问题(以确保不是操作系统问题)
- 清除了我手机上的缓存,禁用了 wifi,并通过具有相同问题的单独 IP 连接
Ugh. I'm starting to think it might be on the server-side, although I'm not sure why it would be, since it was working 100% fine when I was using PHP.
啊。我开始认为它可能在服务器端,虽然我不确定为什么会这样,因为当我使用 PHP 时它 100% 工作正常。
回答by Daniel
It looks like I had forgotten to change the method I was using to login via AJAX. I was only using the JSONP method on the data calls after being logged in. When using CORS, it seems to keep the cookie data separate and so the remote server was not reading the cookie data properly.
看起来我忘记更改我用来通过 AJAX 登录的方法。登录后,我只在数据调用上使用 JSONP 方法。使用 CORS 时,似乎将 cookie 数据分开,因此远程服务器没有正确读取 cookie 数据。
TLDR; Don't mix CORS and JSONP together - make sure to use one or the other to ensure you don't run into this issue yourself!
TLDR;不要将 CORS 和 JSONP 混合在一起 - 确保使用其中之一以确保您自己不会遇到此问题!

