0

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..

JAAulde
  • 19,250
  • 5
  • 52
  • 63
Steve
  • 522
  • 3
  • 12
  • 28
  • Can you also include what the response looks like? Does the page show any errors in the console? – Travis J Dec 22 '13 at 04:37
  • It doesn't shows any error on console. I even debugged the same code using ASP.net and saw its value. The value of readyState always remains 1 and status remains 0. – Steve Dec 22 '13 at 04:41
  • Readystate 1 indicates an open connection. Which makes sense since you alert before the `send` is issued. – Travis J Dec 22 '13 at 04:44
  • The MDN (Mozilla Developers Network) has a lot of valuable information regarding javascript. Check out this [XMLHttpRequest article](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) – Travis J Dec 22 '13 at 04:45
  • Yes. Thank you. But alerting after send changes readyState to 4 and status 404 – Steve Dec 22 '13 at 04:50
  • -The 404 indicates your url is incorrect for the request. Perhaps you should review which url to use, or where to place it. – Travis J Dec 22 '13 at 04:53
  • Why are you passing null parameter in send? you can just call send() Have you tried whether request has reached server side? Just add log on server side, may be url is incorrect – Raunak Kathuria Dec 22 '13 at 05:02
  • yes but this page in the same directory #Travis J – Steve Dec 22 '13 at 05:06
  • I tried send(), but it still shows status as 404 #Raunak – Steve Dec 22 '13 at 05:07

1 Answers1

0

you have to fix the xhrobj.open("GET",str);, the second parameter of the open method, which here is str, 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 added Access-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 call send() method without passing null as an argument but passing null would guarantee your code for all browsers.

Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
  • but then how should i retrieve data from a textbox. – Steve Dec 22 '13 at 05:50
  • if you want to send your data to the server and read some data based on the value of textbox send your text box value as a query-string parameter and read it in your server-side, for instance, in your php code or whatever you use for your server-side, then retrieve your data from database or whatever you use for storing your data and then send it back to your client side, which is waiting for your server-side response, with its `onreadystatechange` callback method. – Mehran Hatami Dec 22 '13 at 05:59
  • I tried to figure out your problem based on what you have said `"how should i retrieve data from a textbox"`, correct me if i haven't understood your mean. – Mehran Hatami Dec 22 '13 at 06:02
  • Thanks to everyone for their valuable response – Steve Dec 22 '13 at 08:14