xmlhttprequest

How come the successful or failure alert never alert?
Only Hello World alerts

function start() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://www.mysite.com/demos/_api/web/lists/getbytitle('IceCreamTrucks')/items", true);
xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
		var jsonObject = JSON.parse(xmlhttp.response);
		alert("Successful");
	}
	else
	{
		alert("Failure");
	}
}
alert("HELLO WORLD");
}

start();

Open in new window


Also, will the default response to an xmlhttprequest going to be xml?
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
You're missing the part that actually sends the request which is why the 'xmlhttp.onreadystatechange' never does anything.  Here's is a simple example from W3Schools: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first  and a list of demos here: http://www.w3schools.com/ajax/ajax_examples.asp   The returned data can be any format though you may have to provide the correct headers for it to work properly.
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
Ahhh....That makes sense.  Thanks!
0
hieloCommented:
>> Also, will the default response to an xmlhttprequest going to be xml?
No.  You can send whatever format you want as the result, but there will be no "automatic conversion".

>> var jsonObject = JSON.parse(xmlhttp.response);
The xmlhttp object does not have a "response" property.  It has a responseText and responseXML.  If you are in fact sending XML markup from the server, then responseXML will have a DOM object(not "raw text") already initialized which you can query for nodes.  If you are not sending XML (or if you believe you are sending XML but the XML contains markup errors, then responseXML will be null, and hence you won't be able to query it for nodes.  
The responseText contains the "raw text", no matter what data format you send from the server.   So, even if you send back XML, responseText will show you the XML markup.  Since seem to be sending json, use responseText (see below).

Also, the readyState changes states from 0 => 1 => 2 => 3 => 4.  So the alert("Failure") needs to be triggered if the readyState==4. So your else clause needs to be within the readyState check and you need to check the status code separately:
var xmlhttp=null;
function start() {
	xmlhttp = new XMLHttpRequest();
 	xmlhttp.open("GET","http://www.mysite.com/demos/_api/web/lists/getbytitle('IceCreamTrucks')/items", true);
 	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState==4 )
	{
		if( xmlhttp.status==200) 
		{
			var jsonObject = JSON.parse(xmlhttp.responseText);
			alert("Successful: " + xmlhttp.responseText);
		}
		else
		{
			alert("Failure");
		}
	}
        xmlhttp.send("");
	alert("HELLO WORLD");
}
start();

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.