Having some trouble with getting the sample code from Mozilla to work with a REST web service in Firefox 3.0.10. Surprisingly, it works fine in IE 8!
- Why is this not working?
- Does IE 8 support XMLHttpRequest? Most examples I've seen use ActiveX allocation. What's the recommended approach? XMLHttpRequest seems more standardized.
Sample:
var req = new XMLHttpRequest();
req.open('GET', 'http://localhost/myRESTfulService/resource', false); // throws 'undefined' exception
req.send(null);
if(req.status == 0)
dump(req.responseText);
The open statement is throwing an exception with the description 'undefined'. Despite allocating the req object and running it in Firefox, it claims to be defined as an 'object' before calling open.
I also tried the asynchronous version of this with no success.
EDIT 2: Here is my latest code:
function createRequestObject() {
if( window.XMLHttpRequest ) {
return new XMLHttpRequest();
}
else if( window.ActiveXObject ) {
return new ActiveXObject( "Microsoft.XMLHTTP" );
}
return null;
}
function handleResponse( req ) {
document.writeln( "Handling response..." ); // NEVER GETS CALLED
if( req.readyState == 0 ) {
document.writeln( "UNITIALIZED" );
}
else if( req.readyState == 1 ) {
document.writeln( "LOADING" );
}
else if( req.readyState == 2 ) {
document.writeln( "LOADED" );
}
else if( req.readyState == 3 ) {
document.writeln( "INTERACTIVE" );
}
else if( req.readyState == 4 ) {
document.writeln( "COMPLETE" );
if( req.status == 200 ) {
document.writeln( "SUCCESS" );
}
}
}
document.writeln( "" );
var req = createRequestObject();
try {
document.writeln( "Opening service..." );
req.onreadystatechange = function() { handleResponse( req ); };
req.open('POST', 'http://localhost/test/test2.txt', true); // WORKS IN IE8 & NOT FIREFOX
document.writeln( "Sending service request..." );
req.send('');
document.writeln( "Done" );
}
catch( err ) {
document.writeln( "ERROR: " + err.description );
}
EDIT 3: Decided to rewrite this using jQuery. While jQuery runs smoothly in IE, it throws 'Undefined' when run from Firefox. Ensured that 'Enable JavaScript' is turned on in Firefox - which works fine on all other websites. Below is the jQuery code:
function handleResponse( resp ) {
alert( "Name: " + resp.Name );
alert( "URL: " + resp.URL );
}
$(document).ready( function() {
$("a").click( function(event) {
try {
$.get( "http://localhost/services/ezekielservices/configservice/ezekielservices.svc/test",
"{}",
function(data) { handleResponse( data ); },
"json" );
}
catch( err ) {
alert("'$.get' threw an exception: " + err.description);
}
event.preventDefault();
});
} ); // End 'ready' check
Summary of Solution:
Lesson learned here. The issue was indeed cross-domain related. Testing the site locally (file system) making requests to a remote service caused the problem. Publishing the site under the same domain resolved this.
This situation highlights an important difference between IE and Firefox. When IE faces such scenarios, it prompts the user for permission for cross-domain calls. On the other hand, Firefox raises an exception. Though exceptions are acceptable, a more informative one would have been beneficial.
Appreciate everyone who assisted me through this problem.