我是初學者網路開發人員。
我在到期和 Laravel 8 中完成了我的第一個專案。
我的 API 回傳錯誤:{"message":"給定的資料無效。","errors":{"email":["Taki email ju\u017c wyst\u0119puje."], "login":["Taki登錄 ju\u017c wyst\u0119puje。"]}}
我有這個代碼:
axios.post(this.$apiAdress '/api/administrators?token=' localStorage.getItem("api_token"),
self.record
)
.then(function (response) {
if (response.data.status == 'success') {
Swal.fire(
'Sukces',
'Rekord dodany poprawnie!@',
'success'
)
} else {
Swal.fire(
'B??d',
response,
'error'
)
}
this.$router.replace({path: '/administrators/create'});
}).catch(function (error) {
if (error.response.data.message == 'The given data was invalid.') {
Swal.fire(
'B??d',
'Prosz? wype?ni? wszystkie pola oznaczone gwiazdk?!',
'error'
)
window.scrollTo({top: 0});
}
在 lin?: error.response.data.message 我有這個錯誤。
我需要在 Swal 中列印錯誤:
Swal.fire(
'B??d',
'.... Here errors ....',
'error'
)
例如:
Swal.fire(
'B??d',
'Taki email ju\u017c wyst\u0119puje. <br/> Taki login ju\u017c wyst\u0119puje.',
'error'
)
我怎樣才能做到?
請幫我
uj5u.com熱心網友回復:
您可以遍歷errors物件:
if (error.response.data.message == 'The given data was invalid.') {
let error_details = ""
for (let key in error.response.data.errors) {
error_details = `${error.response.data.errors[key]}<br/>`
}
Swal.fire(
'B??d',
error_details,
'error'
)
// ...
}
uj5u.com熱心網友回復:
這應該適用于這個特定的例子:
Swal.fire(
'B??d',
error.response.data.email "<br />" error.response.data.login,
'error'
)
uj5u.com熱心網友回復:
您正在列印整個物件,結果如下:
"[object Object]"
您需要從物件中獲取屬性。
response.error.email
// Taki email ju\u017c wyst\u0119puje. <br/> Taki login ju\u017c wyst\u0119puje.
或者將物件列印為字串:
JSON.stringify(response);
您還可以獲得特定的錯誤。
var errors = JSON.parse('{"message":"' response.message '","errors":' JSON.stringify(response.errors) '}');
// Object { ... }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/345981.html
標籤:javascript Vue.js
