角获取请求错误,但只在safari iOS上

时间:2022-08-24 10:26:20

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:

我使用角从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上,如果我记录错误参数,会得到以下响应:

角获取请求错误,但只在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:

一些额外的信息如要求:

1 个解决方案

#1


16  

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上。我敢肯定,如果你的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,进入“偏好”选项,在“价格”选项卡下检查是否有选项:“从第三方和广告商”设置为“阻止cookies和其他网站数据”。这是Safari的第一个默认选项。

In your MacBook it will look like this:

在你的MacBook上,它会是这样的:

角获取请求错误,但只在safari iOS上

And in iOS it will look like this:

在iOS中会是这样的:

角获取请求错误,但只在safari iOS上

Just to confirm that this is in fact what's causing your issue: change this setting to "Never", clear the cache and try to reproduce that problem again. I'm quite confident that you won't be able to reproduce it.

要确认这实际上是导致您的问题的原因:将此设置更改为“Never”,清除缓存,并尝试再次重现该问题。我很有信心你不能复制它。

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 cache every time that you test this.

记住,每次您想要重新测试这个问题时,最好清除保险箱的缓存。否则,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.

我希望这能有所帮助。

#1


16  

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上。我敢肯定,如果你的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,进入“偏好”选项,在“价格”选项卡下检查是否有选项:“从第三方和广告商”设置为“阻止cookies和其他网站数据”。这是Safari的第一个默认选项。

In your MacBook it will look like this:

在你的MacBook上,它会是这样的:

角获取请求错误,但只在safari iOS上

And in iOS it will look like this:

在iOS中会是这样的:

角获取请求错误,但只在safari iOS上

Just to confirm that this is in fact what's causing your issue: change this setting to "Never", clear the cache and try to reproduce that problem again. I'm quite confident that you won't be able to reproduce it.

要确认这实际上是导致您的问题的原因:将此设置更改为“Never”,清除缓存,并尝试再次重现该问题。我很有信心你不能复制它。

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 cache every time that you test this.

记住,每次您想要重新测试这个问题时,最好清除保险箱的缓存。否则,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.

我希望这能有所帮助。