Laravel 5.4中的Access-Control-Allow-Origin标头响应不适用于POST [英] Access-Control-Allow-Origin header response in Laravel 5.4 not working for POST

查看:346
本文介绍了Laravel 5.4中的Access-Control-Allow-Origin标头响应不适用于POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照的说明进行操作这篇文章并将以下内容应用到我的Laravel 5.4后端,后端是我的Angular Web客户端的REST API。

I followed the instructions from this post and applied the following to my Laravel 5.4 backend which is a REST API for my Angular web client.

首先我安装了Cors中间件

First I installed the Cors middleware

php artisan make:middleware Cors

我去了app / Http / Middleware / Cors.php并添加了两个标题:

I went to app/Http/Middleware/Cors.php and added the two headers:

public function handle($request, Closure $next)
{
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}

我添加'cors'=> \ App\Http\Middleware \ Cors :: class $ routeMiddleware in app / Http / Kernel .PHP

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'cors' => \App\Http\Middleware\Cors::class,
];

最后添加中间件('cors')到api路由映射如下:

and finally added middleware('cors') to the api routes mapping as such:

protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware('api')
         ->middleware('cors')
         ->namespace($this->namespace)
         ->group(base_path('routes/api.php'));
}

但是,只有我的 GET 请求正在运行:

However, only my GET request is working:

onSubmit() {
  // Fails: "No 'Access-Control-Allow-Origin' header is present.."
  console.log('Submit ..');
  this.http.post('http://localhost:8080/api/register', JSON.stringify(this.registerForm.value))
    .subscribe(
        data => alert('yay'),
        err => alert(err.error.message)
    );
}

onCancel() {
  // Is working..
  console.log('Cancel ..');
  this.http.get('http://localhost:8080/api/helloworld')
    .subscribe(
      data => alert(data),
      err => alert('error')
    );
}

知道为什么只有 GET 请求有效,但不是 POST

Any idea why only the GET request works but not the POST?

这个我是如何在 api.php 文件中创建路线的:

This is how I create the routes in the api.php file:

Route::get('/helloworld', function (Request $request) {
    return ['Hello World!'];
});

Route::post('/register', function (Request $request) {
    // dd($request->input("email"));
    return ['register'];
});

GET 电话的响应:

Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:no-cache, private
Connection:close
Content-Type:application/json
Date:Sat, 28 Oct 2017 15:14:27 GMT
Host:localhost:8080
X-Powered-By:PHP/7.0.22-0ubuntu0.16.04.1

回复 POST 致电:

Allow:POST
Cache-Control:no-cache, private
Connection:close
Content-Type:text/html; charset=UTF-8
Date:Sat, 28 Oct 2017 15:10:30 GMT
Host:localhost:8080
X-Powered-By:PHP/7.0.22-0ubuntu0.16.04.1

如您所见,由于某种原因,标题未在<$ c $中设置c> POST 回复。

as you can see, for some reason the headers are not set in the POST response.

推荐答案

我想你也需要添加这个

- >标题('Access-Control-Allow-Headers','Content-Type,Authorization,X-Requested-With');

这篇关于Laravel 5.4中的Access-Control-Allow-Origin标头响应不适用于POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆