【问题标题】:Laravel issue No 'Access-Control-Allow-Origin'Laravel 问题没有“访问控制允许来源”
【发布时间】:2017-06-08 16:28:49
【问题描述】:

我正在写一个非常常见的主题,但我很绝望,浪费了两天时间试图完成这项工作。

我在 Chrome 上,尝试制作我的第一个登录/注册应用程序,在将数据发送到后端时,我收到以下错误:

XMLHttpRequest 无法加载 http://192.168.1.133:8100/api/login。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:8100' 不允许访问。响应的 HTTP 状态代码为 500。

我正在从 AngularJS 发出 POST 请求,如下所示:

$scope.data = "UserName"; // for example

$scope.register = function() {

      var withAjax = $http.post('http://192.168.1.133/api/register', $scope.data);

      withAjax
        .success(function(answer){
           console.log(answer.message);
      })
        .error(function(answer) {
          console.log("error");
        });
    }
])

而且 Laravel API 后端很简单,只需返回 'Correct'。

public function login (Request $request) {

  $inputArray = $request->all();

  return response()->json(["message" => "Login: All Correct"]);
}

在我的前端应用程序中,我允许所有类型的连接:

<allow-navigation href="http://*/*" />
<allow-navigation href="*" />
<access origin="*"/>

在 PHP 中我尝试了所有。禁用 API 的 CSFR 令牌,并正确安装“barryvdh/laravel-cors”并配置如下的 cors 文件:

return [
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'],  // ex: ['GET', 'POST', 'PUT',  'DELETE']
    'exposedHeaders' => [],
];

当我可以使来回发送数据的过程变得简单时,我会关心保护。现在我决定打开“全部”以使其简单明了。

我对此很着迷,为了解决这个问题而浪费了两天时间。 任何线索都会有所帮助。

谢谢!

【问题讨论】:

  • 尝试使用邮递员执行请求,看看是否可以从那里完成。
  • 另外,请张贴您的路线以及放置位置

标签: laravel


【解决方案1】:

试试这个 Chrome 插件,可能对你有帮助。 CORS

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 1970-01-01
    • 2018-10-18
    • 2017-07-21
    • 2021-07-18
    • 2020-10-16
    • 2013-11-15
    相关资源
    最近更新 更多