我使用JWT创建了一个登录API,我使用Postman测试它并通过终端curl,它运行良好。但是,当我试图使用jQuery ajax工作时,它会以
响应POST http://router.dev/api/login 401(未经授权)
这假设是在登录时生成令牌。这是ajax代码:
var authenticate = function(){
return $.ajax({
type: 'POST',
dataType: 'json',
processData: false,
contentType: false,
data: {email:'apitest@example.com',password:12346},
url: 'http://router.dev/api/login'
});
}
这是我正在使用的auth方法:
public function authenticate(Request $request){
// grab credentials from the request
$credentials = $request->only('email', 'password');
//dd($credentials);
try {
config(['auth.providers.users.model' => \App\Usuarios::class]);
$token = JWTAuth::attempt($credentials);
// dd($token);
// attempt to verify the credentials and create a token for the user
if (! $token) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(compact('token'));
}
也许这有点沉闷。提前谢谢。