【发布时间】:2022-01-20 10:04:54
【问题描述】:
我正在使用 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
];
}
然后我发送数据表单Next.Js:
使用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))
没问题,但是我发了一个用过的邮箱,会通过422码,axios抓不到结果
使用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))
也可以,但使用邮箱时,会发送302码,并调用索引/
【问题讨论】:
-
“会通过422码,axios抓不到结果”——为什么不在axios的promise链中加一个
.catch(...)呢?跨度>
标签: javascript laravel axios next.js fetch-api