JQuery AJAX error in IE8 with the message "Error: this method cannot be called until the open method is called."

I am using jQuery 1.4.2 and trying to execute a simple AJAX request. The destination URL returns a JSON string (I checked it with jslint). The request works in Firefox and Chrome, but doesn’t want to work in IE8, and I can’t determine why. Here is the challenge:

jQuery.ajax({ url: 'http://' + domain + '/' + 'helper/echo/', dataType: 'json', success: function(data) { alert(data); }, beforeSend: function(request, settings) { alert('Beginning ' + settings.dataType + ' request: ' + settings.url); }, complete: function(request, status) { alert('Request complete: ' + status); }, error: function(request, status, error) { alert(error); } }); 

IE will execute the beforeSend callback and the error callback. Error warnings with message:

 Error: This method cannot be called until the open method has been called. 

My response header is returned with Content-Type: text/javascript; charset=UTF-8 Content-Type: text/javascript; charset=UTF-8 .

What is happening with IE? I start the server on localhost by making a request http: // localhost: 8080 / psx to http: // localhost: 8080 / helper . Maybe IE is blocking this request? I tried installing Fiddler to analyze request traffic, but it will not work on my machine because it is more likely blocked. Firebug allows me, but everything is fine there.

Thanks for the help!!!

+6
json jquery ajax ie-developer-tools
source share
1 answer

Ok, here is the fix! The request used window.XMLHttpRequest() , which for some reason does not work properly in IE8. jQuery does not return to window.ActiveXObject("Microsoft.XMLHTTP") , as it should be.

Add this to your script somewhere before calling AJAX (only in IE8, not other IEs):

 jQuery.ajaxSetup({ xhr: function() { //return new window.XMLHttpRequest(); try{ if(window.ActiveXObject) return new window.ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { } return new window.XMLHttpRequest(); } }); 

This is how I arrived at the solution:

  • Updated jQuery 1.4.4 in case the problem has been fixed.
  • Passed through the Firebug debugger and the DevTools debugger until the results seemed to change.
  • On line 5899, the ajax () function creates an XmlHttpRequest object using the xhr () function. In Firefox, it returned good data. In IE, this returned with all Error: This method cannot be called until the open method has been called. fields Error: This method cannot be called until the open method has been called.
  • I analyzed this function on line 5749, return new window.XMLHttpRequest();
  • I found googled and ran into this page that has the same problem and suggested a solution that works for me.
  • Official jQuery Ticket :
+14
source share

All Articles