本文介紹了responseText - XMLHttpRequest的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在我的代碼中 responseText 不起作用.它應該顯示,在文本框中輸入的文本+:您的請求已被 syam 看到"
<html><head id="Head1" runat="server"><標題></標題><腳本類型="文本/javascript">var xmlHttpRequest;函數 sSignature(str) {xmlHttpRequest = 新的 XMLHttpRequest();xmlHttpRequest.onreadystatechange = function() {如果(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){document.getElementById("target").innerHTML = xmlHttpRequest.responseText;}}xmlHttpRequest.open("GET", "AjaxResponse.aspx?q=" + str, true);xmlHttpRequest.send();}</腳本></頭><身體><form id="form1" runat="server">輸入一個字符串:<input type="text" id="textbox" onkeyup="sSignature(this.value)"/><span id="target">這里的文字應該改變</span></div></表格></身體></html>在代碼隱藏頁面中,在 page_load() 中
string sRequest = Request.QueryString["q"];var sResponse = sRequest+ " :您的請求已被 syam 看到";Response.Write(sResponse);
解決方案 我相信錯誤出在您的 onreadystatechangedhandler
中.它將接收一個 event
參數,其中 target
屬性指向 XHR 實例.
嘗試用這個換掉它:
xmlHttpRequest.onreadystatechange = 函數(事件){var xhr = event.target;如果(xhr.readyState === 4 && xhr.status === 200){document.getElementById("target").innerHTML = xhr.responseText}};
in my code responseText is not working. It is supposed to display, text entered in the text box +" :Your request has been seen by syam"
<html>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
var xmlHttpRequest;
function sSignature(str) {
xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
document.getElementById("target").innerHTML = xmlHttpRequest.responseText;
}
}
xmlHttpRequest.open("GET", "AjaxResponse.aspx?q=" + str, true);
xmlHttpRequest.send();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
enter a string :<input type="text" id="textbox" onkeyup="sSignature(this.value)"/>
<span id="target">text should change here</span>
</div>
</form>
</body>
</html>
In the code-behind page, in page_load()
string sRequest = Request.QueryString["q"];
var sResponse = sRequest+ " :Your request has been seen by syam";
Response.Write(sResponse);
解決方案 I believe the error is in your onreadystatechangedhandler
. It will receive an event
param, in which the target
property points to the XHR-instance.
Try swapping it out with this:
xmlHttpRequest.onreadystatechange = function (event) {
var xhr = event.target;
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}
};
這篇關于responseText - XMLHttpRequest的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!