我有一個錯誤:
422 (Unprocessable Content)
當我嘗試在注冊表中發布資料時
我的 Register.vue 中有這個
methods: {
register() {
this.$store.dispatch('register', {
firstname: this.firstname,
lastname: this.lastname,
email: this.email,
password: this.password,
});
},
},
};
在我的 vuex 中
actions: {
register(credentials) {
const requestOptions = {
method: 'POST',
headers: { 'content-type': 'application/json' },
dataType: 'json',
body: JSON.stringify(credentials),
};
console.log(credentials);
return fetch('http://localhost/api/users', requestOptions)
.then((response) => {
return response.json;
})
.catch((err) => console.log(err.message));
},
}
有誰知道我錯在哪里?非常感謝
uj5u.com熱心網友回復:
如果請求是好的,不要回傳兩次。
actions: {
register(credentials) {
const requestOptions = {
method: 'POST',
headers: { 'content-type': 'application/json' },
dataType: 'json', // not needed
body: JSON.stringify(credentials),
};
console.log(credentials);
return fetch('http://localhost/api/users', requestOptions) // first time
.then((response) => {
return response.json; // second time
})
.catch((err) => console.log(err.message));
},
}
也使用異步/等待。我會做
actions: {
async register(credentials) {
const requestOptions = {
method: 'POST',
headers: { 'content-type': 'application/json' },
dataType: 'json',
body: JSON.stringify(credentials),
};
const res = await fetch('http://localhost/api/users', requestOptions);
}
return res.json();
}
而在 Register.vue
methods: {
register() {
this.$store.dispatch('register', {
firstname: this.firstname,
lastname: this.lastname,
email: this.email,
password: this.password,
}).then(data => {
console.log(data)
}).catch((err) => console.log(err.message));
});
},
};
你也可以把它放在 try/cache 等中。這取決于你。
檢查檔案,它很好地解釋了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402891.html
標籤:
上一篇:將字串格式、影像和標簽的資料轉換為串列/numpy陣列
下一篇:Vuetifyv-btn文本行為
