简体   繁体   English

Laravel API和Ionic 2客户端:CORS没有“ Access-Control-Allow-Origin”标头

[英]Laravel API and Ionic 2 Client: CORS No 'Access-Control-Allow-Origin' header

I created an API with Laravel and I try to use it with an Ionic 2 project 我使用Laravel创建了一个API,并尝试将其用于Ionic 2项目

I'm trying to make a GET request to my Laravel API. 我正在尝试向我的Laravel API发出GET请求。

When I make my http request to 当我向http请求

auth/{prodiver}/callback/{userID}/{accessToken}

I got this error: 我收到此错误:

Failed to load http://api.url.net/api/auth/facebook/callback/XXXX/MY_TOKEN: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8001' is therefore not allowed access.

My route looks like this: 我的路线如下所示:

Route::group(['middleware' => ['guest', 'cors']], function() {
    Route::get('test', function () {
        return response()->json('{dummy.data}', 200);
    });

    Route::get('auth/{prodiver}/callback/{userID}/{accessToken}', 'SocialAuthController@callback')->middleware('cors')->name('social.auth'); // social auth
});

But when I try the same code, with the route test , it works and I got the data... 但是当我尝试相同的代码时,使用路由test ,它可以工作,并且我得到了数据...

The "facebook connect" isn't the problem, the success callback is triggered. “ facebook连接”不是问题,触发成功回调。

Here is the callback() method 这是callback()方法

public function callback($provider, $userID, $accessToken)
{
    if ($provider == "facebook"){
        $user = $this->createOrGetUser($userID, $accessToken);
        auth()->login($user);
        return response()->json($user, 200);
    }
}

All other http requests works perfectly... 所有其他http请求都可以正常运行...

Here is the concerned http request (in Ionic project) 这是有关的http请求(在Ionic项目中)

let headers = new Headers(); 
headers.append('Content-Type', 'application/json'); 
let url = this.baseApiUrl+'auth/facebook/callback/' + userID + '/' + accessToken; 
this.http
    .get(url, {headers: headers})
    .map(res => res.json())
    .subscribe(data => {
            console.log(data)
        }, err => {
            this.onError(err);
        }
    );

I don't understand what's going on, anyone has a clue ? 我不知道这是怎么回事,有人知道吗? I'm totally tired of this error :/ 我完全厌倦了这个错误:/

Thanks a lot! 非常感谢! :) :)

First, why do you set cors middleware when defining the second route? 首先,为什么在定义第二条路线时设置cors中间件? It is already set for the whole group. 已经为整个组设置了。

Second, cors returns a cors error in case you have some error in your code, that's its behaviour. 其次, cors您的代码有错误, cors将返回cors错误,这就是它的行为。

When an error occurs, the middleware isn't run completely. 发生错误时,中间件无法完全运行。 So when this happens, you won't see the actual result, but will get a CORS error instead. 因此,发生这种情况时,您将看不到实际结果,但会收到CORS错误。

Try to debug your code to find a bug. 尝试调试代码以查找错误。 Test returns data, it means that this is not the cors who gives the error, it is your code. 测试返回数据,这意味着错误不是由cors的,而是您的代码。

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

相关问题 离子应用CORS,不带'Access-Control-Allow-Origin'标头,将Lumen API与CORS中间件一起使用 - Ionic app CORS No 'Access-Control-Allow-Origin' header is present, using Lumen API with CORS middleware Laravel 生产,CORS 无“访问控制允许来源” header - Laravel Production, CORS No 'Access-Control-Allow-Origin' header Cors header “访问控制允许来源”被阻止 Api 响应 - Cors header “Access-control-allow-origin” blocked Api Response Laravel Cors Access-Control-Allow-Origin - Laravel Cors Access-Control-Allow-Origin Laravel 上的 CORS (Access-Control-Allow-Origin) - CORS (Access-Control-Allow-Origin) on Laravel 没有“Access-Control-Allow-Origin”标头 - Laravel - No 'Access-Control-Allow-Origin' header - Laravel header('Access-Control-Allow-Origin:*'); 不允许CORS请求 - header('Access-Control-Allow-Origin: *'); Not allowing CORS request “CORS:不存在‘Access-Control-Allow-Origin’header”,但存在 - “CORS: No 'Access-Control-Allow-Origin' header is present ”, but it is present 缺少CORS标头“ Access-Control-Allow-Origin”-PHP,角度 - CORS header 'Access-Control-Allow-Origin' missing - PHP, Angular 标题CORS“Access-Control-Allow-Origin”缺少Codeigniter - header CORS “Access-Control-Allow-Origin” missing Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM