简体   繁体   English

Laravel 5 API和离子1(角度1)的CORS问题

[英]CORS issue with Laravel 5 api and ionic 1 (Angular 1)

Front-end application in Angular 1 and My APIs are built on Laravel and located in different server then sure i will face Cross Origin Request Sharing (CORS) problem and my APIs will not return expected results. Angular 1和My API中的前端应用程序是基于Laravel构建的,并且位于不同的服务器中,因此请确保我将遇到跨源请求共享(CORS)问题,并且我的API将不会返回预期的结果。

I faced the issue and according to some solutions I added some (following code)headers on index.php of Laravel but still I was not able to solve the issue. 我遇到了这个问题,根据一些解决方案,我在Laravel的index.php上添加了一些(以下代码)标头,但仍然无法解决该问题。

header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin:*');

header('Access-Control-Allow-Methods: *'); header('Access-Control-Allow-Methods:*');

header('Access-Control-Allow-Headers: *'); header('Access-Control-Allow-Headers:*');

ERROR Message: on chrome browser, 错误消息:在Chrome浏览器上,

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:8100 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:8100 '。

You should not modify your headers manually at index.php. 您不应该在index.php上手动修改标题。 It is possible that Laravel overrides headers, moreover you cannot determine when your header() function will be called (if even). Laravel可能会覆盖标头,此外,您无法确定何时调用header()函数(如果是偶数)。

You could implement some kind of interceptor that captures every request and sets CORS headers. 您可以实现某种拦截器来捕获每个请求并设置CORS标头。 Or you can try some library like https://github.com/barryvdh/laravel-cors 或者您可以尝试一些类似于https://github.com/barryvdh/laravel-cors的

Are your headers only being added to a GET/POST? 您的标头仅被添加到GET / POST吗? Because the problem is in the preflight OPTIONS request - you need to ensure that at a minimum , the Access-Control-Allow-Origin response header is returned for OPTIONS requests. 因为问题出在预检OPTIONS请求中-您需要确保至少要为OPTIONS请求返回Access-Control-Allow-Origin响应标头。

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

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