得到空响应 [英] get null response
问题描述
hi
这是我的ajax代码
[HTML]< html>
< head>
< script type =" text / javascript">
var req = null;
var console = null;
函数sendRequest (url,HttpMethod)
{
if(!HttpMethod)
{
HttpMethod =" GET" ;
}
req = initXMLHTTPRequest();
if(req)
{
req.onreadystatechange = onReadyState;
req.open(HttpMethod,url,true);
req.send(null);
}
}
函数initXMLHTTPRequest()
{
var xRequest = null;
if(window.XMLHttpRequest)
{
xRequest = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
xRequest = new ActiveXObject
(" Microsoft.XMLHTTP");
}
返回xRequest;
}
函数o nReadyState()
{
var ready = req.readyState;
var data = null;
if( ready == 4)
{
if(req.status == 200)
data = req.responseText;
}
其他
{
data =" loading ... [" + ready +"]" ;;
}
toConsole(数据);
}
函数toConsole(数据)
{
if(console!= null)
{
var newline = document.createElement(" div");
console.appendChild(换行符);
var txt = document.createTextNode(data);
newline.appendChild(txt);
//console.innerHTML=data;
}
}
函数abc()
{
console = document.getElementById(" console");
sendRequest(" ajax2.jsp?name =" + encodeURIComponent(u ser.value)) ;
}
< / script>
< / head>
< body& gt;
< p>
< input type =" text"值= QUOT;"名称= QUOT;使用者" onchange =" javascript :abc()">< / p>
< div id =" console">< / div>
< / body>
< / html> [/ HTML]
&这是我的jsp代码
hi
This my ajax code
[HTML]<html>
<head>
<script type="text/javascript">
var req=null;
var console=null;
function sendRequest(url,HttpMethod)
{
if (!HttpMethod)
{
HttpMethod="GET";
}
req=initXMLHTTPRequest();
if (req)
{
req.onreadystatechange=onReadyState;
req.open(HttpMethod,url,true);
req.send(null);
}
}
function initXMLHTTPRequest()
{
var xRequest=null;
if (window.XMLHttpRequest)
{
xRequest=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xRequest=new ActiveXObject
("Microsoft.XMLHTTP");
}
return xRequest;
}
function onReadyState()
{
var ready=req.readyState;
var data=null;
if (ready==4)
{
if(req.status==200)
data=req.responseText;
}
else
{
data="loading...["+ready+"]";
}
toConsole(data);
}
function toConsole(data)
{
if (console!=null)
{
var newline=document.createElement("div");
console.appendChild(newline);
var txt=document.createTextNode(data);
newline.appendChild(txt);
//console.innerHTML=data;
}
}
function abc()
{
console=document.getElementById("console");
sendRequest("ajax2.jsp?name="+encodeURIComponent(u ser.value));
}
</script>
</head>
<body>
<p>
<input type="text" value="" name="user" onchange="javascript:abc()"></p>
<div id="console"></div>
</body>
</html>[/HTML]
& this is my jsp code
推荐答案
可能是request.status无法访问200?在这种情况下,你将得到一个空值(当你编程它:: data = null)...可能是请求被中止?
亲切的问候
may be the request.status doesn''t get to 200? in that case you will get a null value (as you programmed it :: data = null) ... may be the request is aborted?
kind regards
嗨感谢您的帮助
但是请告诉我现在该怎么办。
我的项目陷入了这一点
请帮助我
提前致谢
hi thanks for help
but please tell me what should I do now.
My project get stuck on this point
please please help me
Thanks in advance
你是否也将glaobal请求对象用于其他请求?您可以使用一种机制,避免在您从已经启动的请求获得响应之前发送新请求,或者您可以使用本地请求变量。
亲切的问候
do you use your glaobal request object for other requests too? you could use a mechanism that avoids to send a new request before you got the response from the already started one or you may use a local request variable.
kind regards
这篇关于得到空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!