[英]Unable to Parse the json object values inside the Ajax call Error section
我對其余的Web API進行了以下Ajax調用:-
$.ajax({
url: "/********/getbytitle('****')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
if(data.d != null){
//code goes here..
}
},
error: function (data) {
alert(data.error.message.value);
$("#customloader").hide();
}
});
在error
部分中的哪里,我想使用alert(data.error.message.value);
捕獲錯誤消息值alert(data.error.message.value);
,但我在瀏覽器控制台中收到以下錯誤:-
data.error.message.value is undefined!!
盡管JSON對象具有以下格式:-
現在,如上圖所述,如果未授權用戶,則返回錯誤。 並且當收到未經授權的請求時,我們的應用程序將顯示用戶名/密碼對話框。.但是我想在用戶名/密碼對話框旁邊顯示警報,並在其中顯示json錯誤消息。
問題是因為error
處理程序的第一個參數是XHR對象,而不是解析的JSON響應。
為了按需進行此工作,您需要自己從XHR獲取responseText
並在嘗試讀取value
屬性之前手動對其進行解析。 嘗試這個:
error: function(xhr) {
var data = JSON.parse(xhr.responseText);
console.log(data.error.message.value);
$("#customloader").hide();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.