[英]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.