javascript Angular GET 请求错误,但仅限于 Safari iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26587400/
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
Angular GET request error, but only on safari iOS
提问by koningdavid
I'm building a website using wordpress as backend, and angularjs as frontend. I'm using the wordpress json API to get my data to the front-end.
我正在使用 wordpress 作为后端和 angularjs 作为前端构建一个网站。我正在使用 wordpress json API 将我的数据发送到前端。
https://wordpress.org/plugins/json-api/
https://wordpress.org/plugins/json-api/
The problem
问题
I'm using angular to get my data from the wordpres json API. I have created the following service:
我正在使用 angular 从 wordpres json API 获取我的数据。我创建了以下服务:
this.getPage = function ( slug ) {
return $http.get('wordpress/api/get_page/?slug=' + slug)
}
I use this service in my controller to get the current page
我在控制器中使用此服务来获取当前页面
HTTPService.getPage('home')
.success(function ( data ) {
$scope.page = data.page;
console.log(arguments);
})
.error( function () {
console.log(arguments);
})
So this is working fine in all browsers, except for safari iOS. On Safari iOS i get the following response if I log the error arguments:
所以这在所有浏览器中都可以正常工作,除了 safari iOS。在 Safari iOS 上,如果我记录错误参数,我会得到以下响应:
This is the safari debugger, I connected my iphone to my macbook. The error response that I get is error code 0..
这是 safari 调试器,我将我的 iphone 连接到我的 macbook。我得到的错误响应是错误代码 0..
What I have tried so far
到目前为止我尝试过的
I have set `Access-Control-Allow-Origin "*" in the htaccess file, but this doesnt seem to work. The request is done on the same domain with a relative URL, so I dont even think that this is the problem.
我在 htaccess 文件中设置了`Access-Control-Allow-Origin "*",但这似乎不起作用。请求是在具有相对 URL 的同一个域上完成的,所以我什至不认为这是问题所在。
So does any1 know why this is not working on safari (iOS only)?
那么有人知道为什么这不适用于 safari(仅限 iOS)吗?
Thanks in advance...
提前致谢...
EDIT
编辑
Some extra info as requested:
根据要求提供一些额外信息:
回答by Josep
I'm pretty sure that this is due to the fact that Safari is the only browser that has the policy of blocking "3rd party cookies and other website data" by default. Actually, this issue shouldn't be exclusive of Safari iOS, it should also happen with Safari on your OSX. I'm pretty sure that if it's not happening in your MacBook is because one day you changed the default settings of the "Privacy".
我很确定这是因为 Safari 是唯一一个默认情况下具有阻止“第三方 cookie 和其他网站数据”策略的浏览器。实际上,此问题不应该是 Safari iOS 独有的,OSX 上的 Safari 也应该会发生此问题。我很确定,如果它没有发生在您的 MacBook 中,是因为有一天您更改了“隐私”的默认设置。
You can try this, open Safari, go to "preferences" and under the tab "Pricacy" check if you have the option: "Block cookies and other website data" set to "From third parties and advertisers". This is the first, and the default option in the modern versions of Safari.
您可以尝试此操作,打开 Safari,转到“首选项”,然后在“隐私”选项卡下检查是否有选项:“阻止 cookie 和其他网站数据”设置为“来自第三方和广告商”。这是第一个,也是现代版本 Safari 中的默认选项。
In your MacBook it will look like this:
在您的 MacBook 中,它将如下所示:
And in iOS it will look like this:
在 iOS 中,它看起来像这样:
Just to confirm that this is in fact what's causing your issue: change this setting to "Never", clear the cacheand try to reproduce that problem again. I'm quite confident that you won't be able to reproduce it.
只是为了确认这实际上是导致您问题的原因:将此设置更改为“从不”,清除缓存并尝试再次重现该问题。我非常有信心你将无法重现它。
Now, if you set it back to "Block cookies and other website data: From third parties and advertisers" and you first clear the cache, you will have that problem again (with either iOS or OSX). After you've confirmed that this is the cause of your problem, set this setting back to "From third parties and advertisers", so that you can reproduce and address the problem with the default settings.
现在,如果您将其设置回“阻止 cookie 和其他网站数据:来自第三方和广告商”并首先清除缓存,您将再次遇到该问题(使用 iOS 或 OSX)。在确认这是导致问题的原因后,将此设置重新设置为“来自第三方和广告商”,以便您可以使用默认设置重现和解决问题。
Bare in mind that every time that you want to re-test this issue you will be better off clearing the cache of Safary. Otherwise it could happen that Safari decides that the site serving the API can be trusted and you won't be able to reproduce the issue. So, just to be sure, clear the cacheevery time that you test this.
请记住,每次您想重新测试此问题时,最好清除 Safari 的缓存。否则,Safari 可能会认为提供 API 的站点是可信的,而您将无法重现该问题。因此,可以肯定的是,每次测试时都要清除缓存。
I believe that the root of this problem is that Safari wants to make sure that the user has had a direct interaction with the page that it's serving the "3rd party content" before the main page loads that content.
我相信这个问题的根源在于 Safari 希望确保用户在主页加载该内容之前与它提供“第三方内容”的页面进行了直接交互。
I would need to know more about your project in order to suggest an "optimal" solution. For instance: will the final app be integrated under the same domain as the API? Because if that's the case, you shouldn't have that issue when you go to production. I mean, if the app that you are developing will be hosted under: http://whatever.yourDomain.org
and the API is going to be part of that same domain (yourDomain.org
), then you shouldn't have that issue at all in production.
我需要更多地了解您的项目才能提出“最佳”解决方案。例如:最终的应用程序是否会与 API 集成在同一域下?因为如果是这样的话,那么当你进入生产环境时就不应该有这个问题了。我的意思是,如果您正在开发的应用程序将托管在:http://whatever.yourDomain.org
并且 API 将成为同一个域 ( yourDomain.org
) 的一部分,那么您在生产中根本不应该遇到这个问题。
On the other hand, if you need to have have the API hosted under a different domain, then you will have to find a way to "trick" Safari. Have a look at this:
另一方面,如果您需要将 API 托管在不同的域下,那么您必须找到一种“欺骗”Safari 的方法。看看这个:
And this:
还有这个:
I hope that this helps.
我希望这个对你有用。