Title: Nothing is returned when asnyc parameter of XHR is set to TRUE
Tags: javascript;ajax;xmlhttprequest
Question: When I was trying a simple code to test XMLHttpRequest() function, I used this code:
<script&gt;
//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&gt;
I want to simply return the contents of my 'test.txt' file.
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!
Comment: @jfriend00. Can you now give me the solution

Comment: @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....!!!!!

Comment: FYI, you should be using `window.onload = getData;`.  You want to pass a function reference, not the return result from executing the function immediately.

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

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

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


Here is another answer: 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.
Comment for this answer: Please explain what is requestObject
Comment for this answer: What if I use `getData()` only? With no: requestObject
Comment for this answer: 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).
Comment for this answer: Just take a look at my code: http://pastebin.com/Rw5udU06 this works and should be easy to understand. Good luck ;)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/17/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><ajax><xmlhttprequest>', 'Title': 'Nothing is returned when asnyc parameter of XHR is set to TRUE', 'ViewCount': 53, 'id': 29689732, 'user_AboutMe': None, 'user_AccountId': 6156487.0, 'user_DisplayName': 'Ravi PK', 'user_Reputation': 23.0, 'user_Views': 4.0, 'user_id': 4799315.0}}