0
如何在HTML5按鈕中插入正確的「xhr」值?使用Javascript和HTML5的XMLHttpRequest
我不知道這整個XMLHttpRequest是如何工作的。我相信這需要:xml數據,文本,數字或HTML5按鈕輸入中的null,並在本例中以文本輸入方式打印出來,但是它可以在其中存儲一個值以稍後調用它。就是那個問題!
<script type="text/javascript">
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;
}
window.document.myform.xhr1.value = data;
return data;
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200); {
window.document.myform.readBodyxhr.value = readBody(xhr);
}
else {
alert(xhr.status);
}
xhr.open('GET', 'http://www.google.com', true);
xhr.send(null);
}
</script>
...HTML5
<input type="button" name="XMLHttpRequest" value="XMLHttpRequest" onclick="readBody(xhr)" />
<input type="text" name="xhr1" value="" size="4"/></td>
<input type="text" name="readBodyxhr" value="" size="4"/></td>
'''預計到.value'是字符串,而不是一個'document','ArrayBuffer'或'Blob'。什麼是預期的迴應? – guest271314
我只是希望能夠將xhr添加爲字符串,數字或其他。 「function readBody(xhr){」。您如何在HTML5代碼中添加「xhr」? – losopha