This is my first code in ajax.......please can anybody help me out....the readySTate and status property is never getting true....it shows xhrobj.readyState=1 and xhrobj.status=0..??
<html>
<head>
<title>MISCELLANEOUS QUESTION NO.1</title>
<script language="javascript">
var xhrobj=false;
if(window.XMLHttpRequest)
xhrobj=new XMLHttpRequest();
else if(window.ActiveXObject)
xhrobj=new ActiveXObject("Microsoft.XMLHttp");
function change1(str)
{
if(xhrobj)
{
var obj=document.getElementById('span1');
xhrobj.open("GET",str);
xhrobj.onreadystatechange=function()
{
if(xhrobj.readyState==4&&xhrobj.status==200)
{
var str1="You Entered...."+str;
obj.innerHTML=xhrobj.responseText;
}
}
var str1="You Entered...."+str;
alert(str1);
alert(xhrobj.readyState);
alert(xhrobj.status);
alert(xhrobj.onreadystatechange);
xhrobj.send(null);
}
}
</script>
</head>
<body>
<form>
<input type="text" id="t1" placeholder="Enter Text...">
<input type="button" onclick="change1(t1.value)" value="FETCH DETAILS OF TEXTBOX">
<br>
</form>
<div id="span1">You Entered....</div>
</body>
</html>
please help me out..
you have to fix the
xhrobj.open("GET",str);
, the second parameter of the open method, which here isstr
, is the URL you want to read your data from and it couldn't read data from anywhere, it should start with the same domain which you'r current page has, unless you have addedAccess-Control-Allow-Origin: *
header to your server response headers,I just changed your exact code with a valid url and it perfectly works. check out your DEMO
BTW, I read some discussions in your comments about passing null argument in send method, when you create a XMLHttpRequest you have to send something, and when your http method is
GET
you have to pass null because it is Http Get, although in some browsers you can just callsend()
method without passing null as an argument but passing null would guarantee your code for all browsers.