Laravel API和离子2客户端:CORS否“访问控制允许来源”头
我创建了Laravel的API和我尝试用离子2项目Laravel API和离子2客户端:CORS否“访问控制允许来源”头
我试图让使用它GET请求到我的Laravel API。
当我把我的http
请求
auth/{prodiver}/callback/{userID}/{accessToken}
我得到这个错误:
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.
我的路线是这样的:
Route::group(['middleware' => ['guest', 'cors']], function() {
Route::get('test', function() {
return response()->json('{dummy.data}', 200);
});
Route::get('auth/{prodiver}/callback/{userID}/{accessToken}', '[email protected]')->middleware('cors')->name('social.auth'); // social auth
});
但是,当我尝试相同的代码,与路线test
,它的工作原理和我得到的数据...
“Facebook连接”不是问题,成功回调触发。
这里是callback()
方法
public function callback($provider, $userID, $accessToken)
{
if ($provider == "facebook"){
$user = $this->createOrGetUser($userID, $accessToken);
auth()->login($user);
return response()->json($user, 200);
}
}
其他所有的http请求的作品完美...
下面是有关http
请求(离子项目)
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);
}
);
我不不知道发生了什么,任何人都有线索?我完全厌倦了这个错误:/
非常感谢! :)
首先,为什么在定义第二条路线时设置了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
谁给出的错误,它是你的代码。
你刚才提到'cors'中间件。你使用什么软件包,'barryvdh/laravel-cors'?你配置正确吗? –
是的,我让配置到处都是“*”,用于开发... – Doddo
[No'Access-Control-Allow-Origin'头文件可能重复 - Laravel 5.4](https://stackoverflow.com/questions/43565877/no-access-control-allow-origin-header-laravel-5-4) – Demonyowh