跨域请求被阻止(原因:CORS预检通道未成功)

时间:2019-05-13 13:24:53

标签: php ajax laravel cors

我有一个Laravel应用程序,除了托管在AWS服务器上的excel Api之外,所有Api都运行良好。

以下是我在excel上传或任何类型的上传中遇到的错误。

  

跨源请求被阻止:同源策略禁止阅读   http://localhost/products上的远程资源。 (原因:CORS   预检通道未成功。

以下标头已添加到 bootstrap / app.php

header('Access-Control-Allow-Origin: http://localhost');
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,PATCH');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Credentials': 'true');

我不确定配置有什么问题,因为为什么只有一个api会与其他api分离而失败。

还附加了ajax脚本

$.ajax({
 type: frm.attr('method'),
 url: 'SERVER_URL',
 data: formData,
 crossDomain: true,
 dataType: "multipart/form-data",
 processData: false,  // tell jQuery not to process the data
 contentType: false,  // tell jQuery not to set contentType
 headers: {
      'Accept':'multipart/form-data',
      'contentType':'Application/json'
  },
  beforeSend: function (xhr) {
   xhr.setRequestHeader("Authorization", "Bearer AUTH_KEY")
 },
 dataType: "json",
 success: function (data) {
         console.log(data);
    },
    error: function (data) {
         console.log(data);
    }
});

下面是网络形象

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:-5)

我认为允许您的本地主机不再安全,然后尝试将 header()设置为

{{1}}