3

I have an application using jQuery Ajax call. It is working fine in IE9,firefox ,chrome. In IE 8 ,it goes in error callback even the response as readyStatus 4 and status 200.

The below response I got in Error handler of IE8

{"readyState":"4","status":"200","statusText":"success"}

The below response I got in success handler in rest of browser including IE9

{
    "Status": "Success",
    "Roles": [
        "Developer",
        "RMG"
    ]
}

Below is my ajax code:

$.ajax({
        cache : false,
        type : "POST",
        async : false,
        url : server + "/Login",
        dataType : "json",
        data : requ,
        contentType : "text/json; charset=UTF-8",
        success : function(dr) {
            data = dr;
            console.log("Success "+JSON.stringify(data));
            alert("Success "+JSON.stringify(data));
        },
        error : function(xhr) {
            alert("Error "+JSON.stringify(xhr));
        }
    }); 

EDIT:

Added this Script:

(function($){
alert("here0");
if (!$.support.cors && $.ajaxTransport && window.XDomainRequest) {
alert("here");
  var httpRegEx = /^https?:\/\//i;
  var getOrPostRegEx = /^get|post$/i;
  var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
  var htmlRegEx = /text\/html/i;
  var jsonRegEx = /\/json/i;
  var xmlRegEx = /\/xml/i;

  // ajaxTransport exists in jQuery 1.5+
  $.ajaxTransport('* text html xml json', function(options, userOptions, jqXHR){
    // XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
    if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(options.url) && sameSchemeRegEx.test(options.url)) {
      var xdr = null;
      var userType = (userOptions.dataType||'').toLowerCase();
      return {
        send: function(headers, complete){
          xdr = new XDomainRequest();
          if (/^\d+$/.test(userOptions.timeout)) {
            xdr.timeout = userOptions.timeout;
          }
          xdr.ontimeout = function(){
            complete(500, 'timeout');
          };
          xdr.onload = function(){
            var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
            var status = {
              code: 200,
              message: 'success'
            };
            var responses = {
              text: xdr.responseText
            };
            try {
              if (userType === 'html' || htmlRegEx.test(xdr.contentType)) {
                responses.html = xdr.responseText;
              } else if (userType === 'json' || (userType !== 'text' && jsonRegEx.test(xdr.contentType))) {
                try {
                    alert("JSON");
                  responses.json = $.parseJSON(xdr.responseText);
                } catch(e) {
                  status.code = 500;
                  status.message = 'parseerror';
                  //throw 'Invalid JSON: ' + xdr.responseText;
                }
              } else if (userType === 'xml' || (userType !== 'text' && xmlRegEx.test(xdr.contentType))) {
                var doc = new ActiveXObject('Microsoft.XMLDOM');
                doc.async = false;
                try {
                  doc.loadXML(xdr.responseText);
                } catch(e) {
                  doc = undefined;
                }
                if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
                  status.code = 500;
                  status.message = 'parseerror';
                  throw 'Invalid XML: ' + xdr.responseText;
                }
                responses.xml = doc;
              }
            } catch(parseMessage) {
              throw parseMessage;
            } finally {
              complete(status.code, status.message, responses, allResponseHeaders);
            }
          };
          // set an empty handler for 'onprogress' so requests don't get aborted
          xdr.onprogress = function(){};
          xdr.onerror = function(){

            complete(500, 'ERROR', {
              text: xdr.responseText
            });
          };
          var postData = '';
          if (userOptions.data) {
            postData = ($.type(userOptions.data) === 'string') ? userOptions.data : $.param(userOptions.data);
          }
          xdr.open(options.type, options.url);
          xdr.send(postData);
        },
        abort: function(){
          if (xdr) {
            xdr.abort();
          }
        }
      };
    }
  });
}

})(jQuery);

But still not working :(

s_k_t
  • 689
  • 1
  • 15
  • 36
  • What is the error message being passed to the error callback? (it's the third parameter) – Kevin B Dec 10 '13 at 18:05
  • yes I am calling a web service and error message is coming from the web services . – s_k_t Dec 10 '13 at 18:08
  • that isn't what i asked. The error callback has three parameters, you are currently only looking at the first one. – Kevin B Dec 10 '13 at 18:08
  • sorry :) it is the third parameter – s_k_t Dec 10 '13 at 18:09
  • so... what is the error message? is it "Error"? is it "parseerror"? is it ""? – Kevin B Dec 10 '13 at 18:10
  • `error : function(xhr,textStatus,errormessage) {alert(errormessage);` – Kevin B Dec 10 '13 at 18:10
  • 2nd parameter is textStatus and value is error 3rd parameter is errormessage and value is No Transport – s_k_t Dec 10 '13 at 18:14
  • is this a cross-origin request? (is `server` == `window.location.href`?) – Kevin B Dec 10 '13 at 18:15
  • yes It is the cross domain request. – s_k_t Dec 10 '13 at 18:16
  • Then that is likely why it's failing. jQuery doesn't support IE8 CORS. – Kevin B Dec 10 '13 at 18:16
  • Ohh i see. Could you please give me a resolution for that , It would be helpful for me as it is a showstopper for me now :( – s_k_t Dec 10 '13 at 18:18
  • http://stackoverflow.com/questions/11487216/cors-with-jquery-and-xdomainrequest-in-ie8-9 – Kevin B Dec 10 '13 at 18:19
  • http://stackoverflow.com/questions/10232017/ie9-jquery-ajax-with-cors-returns-access-is-denied – Kevin B Dec 10 '13 at 18:20
  • Also note that IE7 cannot do CORS requests. – Kevin B Dec 10 '13 at 18:20
  • I have heard about JSONP . Is it also the solution for ajax call in IE8. I am new in jquery and we development so please you help would be appreciated. – s_k_t Dec 10 '13 at 18:49
  • jsonp will work in all browsers, but your remote server has to support it. And, it can't be async: false or type POST. – Kevin B Dec 10 '13 at 18:55
  • When I used the JQueryXDOmainRequest.js plugin . it is giving the error error : function(xhr,textStatus,errormessage) , xhr:{{"readyState":"4","responseText":"","status":"500","statusText":"error"}} and testStatus and errormessage both are error. It should work also for asyn =false request ? – s_k_t Dec 10 '13 at 20:04
  • Yes, should work with async false. status 500 typically means there is a server error. – Kevin B Dec 10 '13 at 20:05
  • if you see the code JqueryXDomainRequest plugin it says xdr.onerror = function(){ complete(500, 'error', { text: xdr.responseText }); }; So as it is working with other browser . it should work with IE 8 also. It can not be a simply server erroe as in plugin it is manually added. – s_k_t Dec 10 '13 at 20:09
  • It is still not working . Facing the problem with the JQueryXDomainRequest Plugin. I need to call a syncronus post call to server. – s_k_t Dec 11 '13 at 14:23

1 Answers1

0

Remove 'console.log(....)'

IE8 doesn't have a console and throws an error every time one is mentioned.

Replace it with alert(...) if you want to see the output . . .

Pat Dobson
  • 3,249
  • 2
  • 19
  • 32