[英]How to access json object which is array
我在我的 ajax 日志記錄表單(Laravel 5.2)中收到以下錯誤響應
{"email":["The email field is required."],"password":["The password field is required."]}
狀態碼為422
我嘗試使用data.responseText.email[0]
訪問 jQuery Ajax 的.fail
塊中的消息,但我收到undefined
錯誤。
我不知道我在這里缺少什么。 簡而言之,我如何訪問消息,以便我可以在 dom 中顯示?
下面是我的失敗塊的片段。
.fail(function(data) {
$('.modal-error-msg').html(data.responseText.email[0]).fadeIn();
})
我假設data.responseText
是一個字符串,而不是一個 Json 對象,你需要將它轉換成一個 Json:
var jsonValue = JSON.parse(data.responseText);
$('.modal-error-msg').html(jsonValue.email[0]).fadeIn();
你可以避免這個問題,如果服務器會返回一條消息
Content-type: application/json
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.