Hi I am trying to work on a script to get back XML text from a php site. However, it is not returning any data. Here is what I am doing.
<head>
<script type="text/javascript">
function getImei()
{
var imei = document.getElementById("imeiInput").value;
if (imei=="")
{
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtResult").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://www.phonesite.com/xml/api/phone.php?IMEI=" + imei,true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="textbox" id="imeiInput"/>
<input type="button" onClick="getImei()" id="btnSubmit" text="Submit"/>
<p id="txtResult"></p>
</body>
</html>
When I do it this way xmlhttp.readystate stops at 1. When I put the “xmlhttp.onreadystatechange = function()” below the xmlhttp.send(), it says the ready state is 4 in FireBug but it completely jumps over the function without going inside of it and finishes.
Also, I did try to enter in the url with the query string directly into my browser and it returned text. Looked at the GET request and they are the same when the Javascript sends it out and when I enter it in.
Date Fri, 14 Oct 2011 01:34:05 GMT
Server Apache
Content-Encoding gzip
Vary Accept-Encoding
Content-Length 87
Keep-Alive timeout=15, max=100
Connection Keep-Alive
Content-Type text/xml
Request Headersview source
Host www.phonesite.com
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
I get this error in firebug for the xml text.
XML Parsing Error: no element found Location: moz-nullprincipal:{baefeeae-cd08-9641-a08f-6b3c3396e8a7} Line Number 1, Column 1:
when you request the responce back from the server to be XML not just text you read that response using responseXML
this will at least return back the text and get you on your way