Laravel 8:302调用POST/PUT方法时重定向



我刚刚安装了Laravel 8,并设置了我的api资源。

但是当我尝试创建/更新记录时,它会将我与302重定向到主页…

  • 这是我的api.php:
Route::apiResource('addresses', AddressController::class);
  • 在我的AddressController.php中,我的store方法:
public function store(CreateAddressRequest $request)
{
return response()->json(Address::create($request->validated()));
}

需要帮助(理解),请。

请设置报头为postman - Accept: application/json.

当您通过store()方法创建时,您有CreateAddressRequest类来验证您的输入。当验证失败时,它会重定向到上一页,如果没有上一页,它可以重定向到主页。

对于API,这种行为是不可取的,因为您希望返回错误消息(在JSON中)而不是重定向。

使用验证器返回错误的示例。验证请求时的REST API