Javascript responseText-XMLHttpRequest
在我的代码中,responseText不起作用。它应该显示文本框+”中输入的文本:syam已看到您的请求Javascript responseText-XMLHttpRequest,javascript,xmlhttprequest,Javascript,Xmlhttprequest,在我的代码中,responseText不起作用。它应该显示文本框+”中输入的文本:syam已看到您的请求 首先发送您的请求 function sSignature(str) { xmlHttpRequest = new XMLHttpRequest(); xmlHttpRequest.open("GET", "AjaxResponse.jsp?q=" + str, true); xmlHttpRequest.send(); xmlHttpRequest.onre
首先发送您的请求
function sSignature(str) {
xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", "AjaxResponse.jsp?q=" + str, true);
xmlHttpRequest.send();
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
document.getElementById("target").innerHTML = xmlHttpRequest.responseText;
}
}
}
我相信错误在您的
onreadystatechangedhandler
中。它将接收一个事件
参数,其中目标
属性指向XHR实例
尝试用以下方法将其替换:
xmlHttpRequest.onreadystatechange = function (event) {
var xhr = event.target;
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}
};
请注意,您的代码在Microsoft Internet Explorer中不起作用 其次,修改一行代码使其看起来更好-
xhr.send()
byxhr.send(空)代码>你能比“不工作”做得更好吗?网络交通检查员怎么说?控制台中是否有错误?您是否尝试查看它在代码中运行了多远后才会出错?当readystate为4 document时,它在行中给出了一个错误。getElementById(“目标”).innerHTML=xmlHttpRequest.responseText;检查你的代码隐藏页面,你是否成功地获取了查询字符串你应该只从回调中获取ResponseText..我认为这对我使用PHP后端是有效的<代码>$sRequest=$\u GET[“q”]$sResponse=$sRequest。“:syam已看到您的请求”;echo$sResponse代码>。因此,请检查您的ASP代码。将xhr.responseCode
更改为xhr.status
为什么使用xhr.send(null)
而不是xhr.send()
?
function sSignature(str) {
xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", "AjaxResponse.jsp?q=" + str, true);
xmlHttpRequest.send();
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
document.getElementById("target").innerHTML = xmlHttpRequest.responseText;
}
}
}
xmlHttpRequest.onreadystatechange = function (event) {
var xhr = event.target;
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("target").innerHTML = xhr.responseText
}
};