Hi guys!
I'm getting insane with this one...
I made a RESTful.NET webservice with C# and is running as a standalone service. When I make a XMLHttpRequest to retrieve JSON data, all browsers fail, except IE, because the status flag of the instance of XMLHttpRequest is always 0 -- it should be 200, that it's what's happening with IE.
I have already read that 0 is the correct value for a static html page who are not on a webserver. So, I installed Apache and put the page there, but the result is the same (I may have wrongly interpreted the suggestion...)
I ran Fiddler and the requests are always correctly retrieved for any browser!
Here's the JavaScript code that I'm using:
<script type="text/javascript">
var serviceURI = 'http://localhost:8889/WebService/client/25';
var xmlHttp = new XMLHttpRequest();
function clientHandler()
{
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
var json = xmlHttp.responseText;
var client_id = document.getElementById('clientid');
var client_name = document.getElementById('clientname');
var client_address = document.getElementById('clientaddress');
var client_phone = document.getElementById('clientphone');
var client = eval('(' + xmlHttp.responseText + ')');
client_id.value = client.id;
client_name.value = client.name;
client_address.value = client.address;
client_phone.value = client.phoneNumber;
}
else
alert("Error:\nxmlHttp.responseText = " + xmlHttp.responseText + "\nstatus = " + xmlHttp.status);
}
function sendRequest()
{
if (xmlHttp)
{
xmlHttp.onreadystatechange = clientHandler;
xmlHttp.open('GET', serviceURI, true);
xmlHttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlHttp.send(null);
}
else
alert('xmlHttp not defined!');
}
</script>
I would be really pleased if someone could explain me what's happening; I haven't found any sactisfatory explnation for this...
Also, is it better to use a framework like jQuery for this job?
Thanks everybody!
PS: I've have consulted Jon Flanders RESTful.NET, but it ain't helping much on solving this... :(
Edit: I moved the answer to this question to a comment bellow!