[英]next.js fetch data from Laravel
i am writing a simple user register using Laravel with a simple code我正在使用 Laravel 和一个简单的代码编写一个简单的用户寄存器
public function register()
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required'
]);
$user = User::create(request(['name', 'email', 'password']));
auth()->login($user);
return [
'status' => true,
'user' => $user
];
}
then i am sending the data form Next.Js
:然后我发送数据表单Next.Js
:
using axios
:使用axios
:
let config = {
method: 'post',
url: `http://localhost:8000/api/register`,
headers: {
'Content-Type': 'application/json',
},
data: values,
}
axios(config)
.then(json => console.log(json))
it's work fine, but i send an used email address, it will through 422 code, and axios can't catch the result它工作正常,但我发送一个使用过的 email 地址,它将通过 422 代码,并且 axios 无法捕获结果
using fetch
:使用fetch
:
fetch('http://localhost:8000/api/register', {
method: "post",
mode: 'no-cors',
body: new URLSearchParams(data)
}).then(res => res.json())
.then(json => console.log(json))
also work but when use used email, it will send 302 code, and call the index /
也可以,但是当使用 email 时,它会发送 302 代码,并调用索引/
I think that you have missed adding the .catch(...)
to return the error object like the example in docs Axios docs我认为您错过了添加.catch(...)
以返回错误 object 就像文档Axios 文档中的示例一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.