Trong XMLHttpRequest
, sử dụng XMLHttpRequest.responseText
có thể nâng cao ngoại lệ như dưới đây
Failed to read the \'responseText\' property from \'XMLHttpRequest\':
The value is only accessible if the object\'s \'responseType\' is \'\'
or \'text\' (was \'arraybuffer\')
Cách tốt nhất để truy cập vào các phản hồi từ XHR như sau
function readBody(xhr) {
var data;
if (!xhr.responseType || xhr.responseType === "text") {
data = xhr.responseText;
} else if (xhr.responseType === "document") {
data = xhr.responseXML;
} else {
data = xhr.response;
}
return data;
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
console.log(readBody(xhr));
}
}
xhr.open('GET', 'http://www.google.com', true);
xhr.send(null);
Nguồn
2015-09-30 07:16:32
thể trùng lặp của [Tôi thiếu gì trong XMLHttp Yêu cầu?] (Http://stackoverflow.com/questions/2482916/what-am-i-missing-in-the-xmlhttprequest) –
nếu bạn mở thư viện JS, jQuery thực sự đơn giản hóa điều này bằng phương thức .load() : http://api.jquery.com/load/ – scunliffe
cảm ơn trời, cuối cùng là kết quả của google không giải quyết được jQuery: | –