Laravel API - AngularJS:访问控制允许来源错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34356454/
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
Laravel API - AngularJS: Access-Control-Allow-Origin error
提问by Jornve
Hello stackoverflowers!
你好stackoverflowers!
I have a hard time to get Angular and Laravel working together. I want to use Angular separately from Laravel. I have setup 2 domains app.domain.com and api.domain.com using forge. When i want to send requests from my app.domain.com or localhost to my api.domain.com i got a 'Access-Control-Allow-Origin' (=CORS?) error.
我很难让 Angular 和 Laravel 一起工作。我想与 Laravel 分开使用 Angular。我已经使用伪造设置了 2 个域 app.domain.com 和 api.domain.com。当我想从我的 app.domain.com 或 localhost 向我的 api.domain.com 发送请求时,我收到了“Access-Control-Allow-Origin”(=CORS?)错误。
Error:
错误:
XMLHttpRequest cannot load http://api.domain.com/api/authenticate. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.
XMLHttpRequest 无法加载http://api.domain.com/api/authenticate。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8888'。
I googled for hours but i can't figure it out. I already tried adding CORS middleware to Laravel but that has no effect.
我用谷歌搜索了几个小时,但我无法弄清楚。我已经尝试向 Laravel 添加 CORS 中间件,但这没有效果。
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Max-Age', '1000')
->header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
}
Error is still being showed. The weird part is that get request are allowed, only post requests are not allowed for some reason.
仍然显示错误。奇怪的部分是允许获取请求,由于某种原因只允许发布请求。
Normally i use APACHE as server but Laravel Forge is using nginx. Does this has something todo with nginx?
通常我使用 APACHE 作为服务器,但 Laravel Forge 使用 nginx。这与 nginx 有关系吗?
My nginx config file:
我的 nginx 配置文件:
server {
listen 80;
server_name api.domain.be;
root /home/forge/api.domain.be/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/api.onlinevaten.be-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
}
Can anyone help me out? Would be awesome!
谁能帮我吗?会很棒!
采纳答案by Jornve
Try This , it worked well for me
试试这个,它对我很有效
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');