简体   繁体   English

离子怪异的“访问控制允许来源”问题

[英]Ionic weird 'Access-Control-Allow-Origin' issue

Hello i built my ionic app, it worked, on a whole lot of devices, then just a couple of hours ago i tried to run the app on my phone and i noticed that for some reason data wasn't being returned from my web server. 您好,我在许多设备上构建了我的离子应用程序,该应用程序可以正常工作,然后就在几个小时前,我尝试在手机上运行该应用程序,但我注意到由于某些原因,没有从Web服务器返回数据。 So i tried to run the app on my computer using ionic serve to see what was wrong and i noticed this error kept popping up every time i tried to make an $http request: 因此,我尝试使用ionic serve在计算机上运行该应用程序以查看出了什么问题,并且我注意到每次尝试发出$ http请求时,该错误都会不断出现:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100'

My app has been running perfectly for weeks now, i do not know what could have changed to cause this. 我的应用程序已经运行了好几个星期了,我不知道是什么原因引起的。 I use google canary for development, i have disabled web security. 我使用Google Canary进行开发,但已禁用网络安全性。 But even if the issue was with my browser that still does not explain why the app doesn't work on my phone in particular now. 但是,即使问题出在我的浏览器上,也仍然无法解释为什么该应用程序现在在我的手机上无法正常运行。 I was thinking it might have something to do with a new android update or something. 我在想这可能与新的android更新有关。

The only changes i have made to my computer recently are uninstalling my norton security antivirus and installing malwarebytes anti-malware and anti-exploit. 我最近对计算机进行的唯一更改是卸载Norton Security防病毒软件并安装了反恶意软件和反利用恶意软件字节。

I have installed laravel-cors on my server. 我已经在服务器上安装了laravel-cors And set the setting at default like this: 并像下面这样默认设置:

<?php

return [
    /*
     |--------------------------------------------------------------------------
     | Laravel CORS
     |--------------------------------------------------------------------------
     |

     | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*') 
     | to accept any value, the allowed methods however have to be explicitly listed.
     |
     */
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['GET', 'POST', 'PUT',  'DELETE'],
    'exposedHeaders' => [],
    'maxAge' => 0,
    'hosts' => [],
];

I currently have no idea what is going on, i use a lenovo A6000 android phone for development. 我目前不知道发生了什么,我使用联想A6000 android手机进行开发。 The android system webview version is 47.0.2526.100, the android OS version is 5.0.2. android系统的webview版本是47.0.2526.100,android OS版本是5.0.2。

I just installed the app on an old nokia x "android phone" with an android version of 4.1 and it works. 我刚刚将应用程序安装在Android版本为4.1的旧诺基亚x“ Android手机”上,并且可以运行。

Let me clarify, by "not working" i mean all $http requests, the app itself opens and works normally. 让我澄清一下,“不工作”是指所有$ http请求,该应用程序本身都会打开并正常工作。

As a quick fix, put plain php headers in the very beginning of index.php 作为快速解决方案,将普通的php标头放在index.php的开头

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Key, Authorization");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, PATCH");

This will work with any laravel version. 这适用于任何laravel版本。 Then figure out laravel and laravel-cors package versions and fix this in a proper way. 然后找出laravel和laravel-cors软件包的版本,并以适当的方式进行修复。

Okay so my issue like I described in my post worked on some devices and not on some devices. 好的,所以我在帖子中描述的问题在某些设备上有效,而在某些设备上无效。 I have no idea why. 我不知道为什么。

As to the cause of the issue, why this started happening, once again i have no idea why. 至于问题的原因,为什么会这样,我再次不知道为什么。

In an attempt to try and resolve the issue i installed CORS with the following default settings: 为了尝试解决此问题,我使用以下默认设置安装了CORS:

<?php

return [
    /*
     |--------------------------------------------------------------------------
     | Laravel CORS
     |--------------------------------------------------------------------------
     |

     | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
     | to accept any value, the allowed methods however have to be explicitly listed.
     |
     */
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'],
    'exposedHeaders' => [],
    'maxAge' => 0,
    'hosts' => [],
];

When the above didn't work I decided to check my server to make sure I had the Headers module installed because i noticed the "Access-Control-Allow-Origin" header wasn't being included in my response headers from the server. 当上述方法不起作用时,我决定检查服务器以确保安装了Headers模块,因为我注意到服务器的响应头中没有包含“ Access-Control-Allow-Origin”头。

I then proceeded to directly configuring my .htaccess file by adding this: 然后,我通过添加以下内容直接配置我的.htaccess文件:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Credentials false
</IfModule>

However the above still did not make any difference at least not untill i commented out the global CORS middleware in my Kernel.php file. 但是以上内容至少没有改变,直到我在Kernel.php文件中注释掉了全局 CORS中间件。

It works perfectly now. 现在,它可以完美运行。 :) :)

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

相关问题 ionic:请求的资源上不存在“ Access-Control-Allow-Origin”标头 - ionic: No 'Access-Control-Allow-Origin' header is present on the requested resource 科尔多瓦没有访问控制允许来源 - cordova No Access-Control-Allow-Origin android webview - 访问控制允许来源 - android webview - Access-Control-Allow-Origin Angular 无“访问控制允许来源” - Angular No 'Access-Control-Allow-Origin' IONIC CORS 的噩梦……签名的 APK 仍然需要“Access-Control-Allow-Origin”才能运行 - IONIC CORS nightmare... Signed APKs still needs "Access-Control-Allow-Origin" to run Android 4.1上的Access-Control-Allow-Origin错误 - Access-Control-Allow-Origin Error At Android 4.1 WebView不存在“ Access-Control-Allow-Origin”标头[ANDROID] - WebView No 'Access-Control-Allow-Origin' header is present [ANDROID] webview-访问控制允许原点不允许原点(使用cordova将angular转换为mobile) - webview - origin is not allowed by access-control-allow-origin (convert angular to mobile using cordova) Android webview - XMLHttpRequest无法加载Origin <url> Access-Control-Allow-Origin不允许使用 - Android webview - XMLHttpRequest cannot load Origin <url> is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许在PhoneGap中使用ng-views获取Origin - using ng-views in PhoneGap getting Origin is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM