【发布时间】:2021-12-27 04:59:15
【问题描述】:
我有下一个 js 应用程序和 Axios 用来调用流明端点。 Axios http 客户端函数被编码到一个单独的文件夹中,称为 services/index.tsx 示例代码,如下所示。
export const register = async (payload: any) => {
try {
const endpoint = baseUrl + '/api/register';
const res = await Axios.post(endpoint, payload)
return await res.data
} catch (error) {
return error.response
}
}
从 signup.tsx 组件调用上述注册函数,如下所示
const formik = useFormik({
onSubmit: (values) => {
register(values).then(response => {
console.log(data);
}).catch(function(error){
console.log(error)
})
}
});
Lumen 控制器 json 响应如下所示。表单验证错误
$this->validate($request,[
'email' => 'required|unique:users,email'
]);
return response()->json(['message' => 'Account Created'],201);
如何在下一个js应用的signup.tsx组件中显示流明控制器响应数据?
【问题讨论】:
-
你有语法错误
register(values).then(response在这里你得到response变量和日志data?如何 -
response.message应该有你想要的数据。将其设置为状态属性以显示它。仅供参考,return await res.data中的await是多余的 -
@KamleshPaul 我可以在 services/index.tsx 文件中获取错误响应。 “error.response.data”,但我想将该错误消息转发到 signup.tsx 组件
-
@aseladaskon 使用
state -
@KamleshPaul 你的意思是
const [error, setState] = useState()
标签: javascript reactjs laravel axios next.js