【发布时间】:2020-09-16 07:34:15
【问题描述】:
当我尝试使用错误的凭据登录时收到此错误:
<!doctype html>
<html class="theme-light">
<!--
TypeError: Argument 2 passed to Symfony\Component\HttpFoundation\JsonResponse::__construct() must be of the type integer, array given, called in /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php on line 31 in file /home/stylmyvz/cars.styleshopeg.com/vendor/symfony/http-foundation/JsonResponse.php on line 42
#0 /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php(31): Symfony\Component\HttpFoundation\JsonResponse->__construct(Array, Array, Array)
#1 /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php(99): Illuminate\Http\JsonResponse->__construct(Array, Array, Array, 0)
#2 /home/stylmyvz/cars.styleshopeg.com/app/Http/Controllers/APILoginController.php(31): Illuminate\Routing\ResponseFactory->json(Array, Array)
#3 [internal function]: App\Http\Controllers\APILoginController->login(Object(Illuminate\Http\Request)
I want to receive JSON response like this in Postman :
{
"error":"invalid username or password"
}
这是登录控制器,我指示它给我“无效的用户名或密码”
谁能告诉我这是怎么回事
class APILoginController extends Controller
{
//
public function login(Request $request){
$validator = Validator::make($request -> all(),[
'email' => 'required|string|email|max:255',
'password' =>'required'
]);
if($validator->fails()){
return response()->json($validator->errors());
}
$credentials = $request->only('email', 'password');
try {
if(! $token = JWTAuth::attempt($credentials))
{
return response()->json(['error'=>'invalid username or password'], [401]);
}
} catch (JWTException $e) {
//throw $th;
return response()->json(['error'=>'could not create token'], [500]);
}
return response()->json(compact('token'));
}
}
api.php 文件内容:
Route::post('user/register','APIRegisterController@register');
Route::post('user/login','APILoginController@login');
Route::middleware('jwt.auth')->get('/users', function (Request $request) {
return auth()->user();
});
Route::middleware('jwt.auth')->group( function(){
Route::resource('/cars', 'API\CarController');
} );
【问题讨论】:
-
你需要从 Postman 发送一个 POST 请求
-
如果您发布您的 route.php 或 api.php 文件的内容会很有帮助
-
@SalimDjerbouh 我确实发送了一个 POST 请求,这就是我发送它时收到的内容
-
@jobvink 我在上面添加了 api.php 文件但我不知道在哪里可以找到 route.php 文件
-
@SalimDjerbouh 我用 POST 请求替换了我得到的错误