繁体   English   中英

CORS - 流明和角度请求

[英]CORS - Lumen and Angular request

我有一个在http://localhost:4200 上运行的 Angular 应用程序和一个在http://localhost:8000 上运行的 Lumen API。

我正在使用 barryvdh/laravel-cors,并且我所有的 POSTS 请求都返回“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”

有什么线索吗? 我的 Angular 数据库服务:

@Injectable()
export class DatabaseService {

  constructor(private http: Http) { }

  get(url: string) {
    return this.http.get("http://localhost:8000/" + url)
      .map(response => response.json());
  }

  post(url: string, data) {
    return this.http.post("http://localhost:8000/"+ url, data)
      .map(response => response.json());
  }

}

所有 GET 请求都正常工作。

启用 apache mod_headers 将解决这个问题。 运行以下命令

  1. a2enmod 头文件
  2. 须藤服务 apache2 重启

这取决于您传递的数据以及您在后端的处理方式。 你有没有尝试过对数据进行字符串化

JSON.stringify(data)

post(url: string, data) {
return this.http.post("http://localhost:8000/"+ url, JSON.stringify(data))
  .map(response => response.json());
}

您需要在 Lumen 上授予对 Angular 服务器的访问权限

如果您不使用 CORS 只需在 /bootstrap/app.php 上添加这一行

header('Access-Control-Allow-Origin: http://localhost:4200/');

或为每个人:

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

如果您使用 CORS,您必须在 App\\Http\\Middleware\\CorsMiddleware(或任何称为您的 CORS 文件)的标头上设置相同的内容

$headers = [
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age'           => '86400',
            'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With'
        ];

暂无
暂无

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

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