我必須從客戶端收到一個POST請求到laravel 5中的REST應用程序。我按照有關驗證的文檔,它表示,當一個AJAX請求laravel不生成重定向響應,而不是生成一個帶有錯誤的JSON響應(https://laravel.com/docs/5.5/validation#a-note-on-optional-fields)。但是,當我提出從REST客戶端我得到這個響應召喚:在Laravel返回其他電話的錯誤消息
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url=http://laraveltest.com:8080/test/public" />
<title>Redirecting to http://laraveltest.com:8080/test/public</title>
</head>
<body>
Redirecting to <a href="http://laraveltest.com:8080/test/public">http://laraveltest.com:8080/test/public</a>.
</body>
</html>
AdultoPost類*********************
class AdultoPost extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'Profesion' => "max:50",
"EstadoCivil" => "required",
"FamiliaReconstituida" => "required",
"SolteroConHijo" => "required",
"Viudo" => "required",
"NoHijos" => "required"
];
}
}
AdultoController **************
public function post(AdultoPost $request, Adulto $adulto, Paciente $paciente, Persona $persona)
{
$persona = Persona::create($request->all());
$adulto = Adulto::create($request->all());
$paciente = Paciente::create($request->all());
return $adulto;
}
如果我改變AdultoPost類請求,並把驗證規則後函數內,趕上一個ValidationException我得到這個錯誤迴應「給定的數據無效」。這不是我想要的。我想向客戶發送哪些字段無效以及爲什麼。
我正在從VSCODE的REST API插件的請求,並從REST API測試擴展由鉻,現在我安裝郵遞員繼續測試
POST http://laraveltest.com:8080/test/public/api/adulto
Content-Type: application/json
Accept: application/json
{
"FechaInicio":"2017-09-10",
"MotivoConsulta":"un motivo real",
"ComoConocio":"como conocio"
}
請註明你如何要求你的REST API。如果你正在做Ajax,你可以在控制檯上查看你的api發送的實際數據結構,但是如果你沒有使用ajax,那麼請在你的html表單和處理你的請求的實際功能代碼上提供你的代碼。 – dexterb