我有一个方法是通过axios生成的PUT来捕获vue视图(Profile.vue)发送的信息,问题在于,当数据更新时(使用UserController的myProfile方法),axios捕获了方法返回json信息,并显示成功信息,但是出现错误时,Axios没有捕获错误json信息,并声称如下:
Uncaught (in promise) TypeError: Cannot read property 'status' of undefined
Run Code Online (Sandbox Code Playgroud)
我知道您向我指控我在 Axios 的 catch 部分中没有信息的变量。
myProfile 代码(用户控制器)
$profile = User::find(Auth::user()->id);
$profile->firstname = $request->firstname;
$profile->lastname = $request->lastname;
$profile->gender = $request->gender;
$profile->description = $request->description;
if($profile->update())
{
return response()->json([
'status' => 'Muy bien!',
'msg' => 'Datos actualizados correctamente.',
'cod' => 201
]);
}
else{
return response()->json([
'status' => 'Ocurrio un error!',
'msg' => 'Ocurrio un error al actualizar la información.',
'cod' => 400
]);
}
Run Code Online (Sandbox Code Playgroud)
Profile.vue 的 Axios 部分
axios.put('/dashboard/profile', value)
.then((response) …Run Code Online (Sandbox Code Playgroud) 我正在占用 vue-datepicker 组件的输入,该输入显示我在表单中的日期。我需要让日期从当天开始,不能选择前几天,还要更改语言,组件不让我添加样式
这是我使用的组件https://github.com/charliekassel/vuejs-datepicker
我留下部分摘要代码:
<template>
<form>
<datepicker :bootstrap-styling="true"
class="form-control"
</datepicker>
</form>
</template>
<sctipt>
import Datepicker from 'vuejs-datepicker';
export default {
components: {
Datepicker,
},
data () {
return {
selectedDate: ''
},
method: {
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我占用了 Vuejs 2 和 vuejs-datepicker
¿我如何使用Axios从Laravel中的控制器方法捕获错误?问题如下,当数据通过laravel中UserController中的myProfile方法的验证程序并且是正确的时,该方法中会生成json响应,然后Axios接收它们并显示toast成功消息,但是当我通过时错误或空的数据发送到验证器,并且失败,Axios不会将带错误的json并显示给我空的吐司,并在控制台中生成错误422。
用户控制器中的myProfile
public function myProfile(Request $request)
{
$valido = $this->validate($request, [
'firstname' => 'required|min:3|max:15',
'lastname' => 'min:2|max:15',
'gender' => 'numeric',
'description' => 'max:200',
]);
if($valido){
return response()->json([
'status' => 'success',
'msg' => 'Ok',
], 201);
}
else{
return response()->json([
'status' => 'error',
'msg' => 'Error',
], 422);
}
}
Run Code Online (Sandbox Code Playgroud)
Profile.vue(Axios部分)
updateUser(){
const value = {
'id': this.user.id,
'firstname': this.user.firstname,
'lastname': this.user.lastname,
'gender': this.user.gender,
'description': this.user.description,
}
axios.put('/dashboard/profile', value)
.then((response) => {
let title = response.data.status;
let body = response.data.msg;
this.displayNotificationSuccess(title, …Run Code Online (Sandbox Code Playgroud)