1

When I was trying a simple code to test XMLHttpRequest() function, I used this code:

<script>
//Global variable to store the XMLHttpRequest object

    var myRequest;

    //Package the request into its own function

    function getData()

    {

    //Feature-testing technique to make sure the browser

    //supports the XMLHttpRequest object

    if (window.XMLHttpRequest)

    {

    //create a new instance of the object

    myRequest = new XMLHttpRequest();

    }

    //else - the XMLHttpRequest object is not supported:

    //create an instance of ActiveXObject

    else

    {

    myRequest = new ActiveXObject("Microsoft.XMLHTTP");

    }

    //Use the open(retrievalMethod, "myDataFile.txt", bool) method

    myRequest.open("GET", "test.txt", true);

    //Use send() with a null argument - we have nothing to send:

    myRequest.send(null);

    //attach a function to handle onreadystatechange,

    //that is, the response from the server:

    myRequest.onreadystatechange = getData;
    alert(myRequest.responseText);
    }
</script>

I want to simply return the contents of my 'test.txt' file.

Now, When I am running this code, I get nothing! I see only a blank screen.....

And now, When i set Asnyc parameter to false, It Works!

Why??

Ravi PK
  • 23
  • 8
  • FYI, you should be using `window.onload = getData;`. You want to pass a function reference, not the return result from executing the function immediately. – jfriend00 Apr 17 '15 at 03:31
  • 1
    This makes no sense. `getData()` is starting an ajax call and you then hook up `onreadystatechange` to getData which will start another ajax call, etc.... This would go on forever if something doesn't cause an error. – jfriend00 Apr 17 '15 at 03:32
  • You'll have to show us the code that actually uses the result of the ajax call because that is probably where your coding error is, but you left that out of your question. – jfriend00 Apr 17 '15 at 03:34
  • @jfriend00. Can you now give me the solution – Ravi PK Apr 17 '15 at 04:32
  • 1
    There's no code in your question that actually tries to do anything with the ajax result. As I said before, we need to see that code. – jfriend00 Apr 17 '15 at 04:45
  • @jfriend00 : Sorry sir! But, when i use `responseText` to get data, then it returns an empty value, that's my problem. Why is this happening? Help me sir....!!!!! – Ravi PK Apr 18 '15 at 02:49

1 Answers1

-1

XMLHttpRequests that point to local files isn't possible out of the box (xmlhttprequest for local files).

Your getData()-method should contain something like this:

funtion getData (requestObject) {
    if (request.readyState === 4) {
         //DO STUFF
    }
}

The reason why it works when you execute the request sync is because you probably didn't include the above code.

Community
  • 1
  • 1
Joram
  • 64
  • 7
  • Please explain what is requestObject – Ravi PK Apr 18 '15 at 01:15
  • The requestObject parameter is your requestObject. An XMLHttpRequest has 4 states (http://stackoverflow.com/questions/632774/what-do-the-different-readystates-in-xmlhttprequest-mean-and-how-can-i-use-them). You have to wait with executing code until your request reaches readyState 4 (which means the request is complete). – Joram Apr 18 '15 at 11:14
  • What if I use `getData()` only? With no: requestObject – Ravi PK Apr 18 '15 at 13:48
  • Just take a look at my code: http://pastebin.com/Rw5udU06 this works and should be easy to understand. Good luck ;) – Joram Apr 20 '15 at 09:23