Ajax GET request not working in IE 8 and 9

3k views Asked by At

I've tried everything under the sun and can't get this working.

Trying to get a simple GET request cross domains in IE 8+9.. works fine in Chrome and Firefox and IE10. Tried using XDomainRequest but no dice.. get undefined error.

function RequestWrapper (url, data, error, success, method) {

// IE8 & 9 only Cross domain JSON GET request
if ('XDomainRequest' in window && window.XDomainRequest !== null) {

    var xdr = new XDomainRequest(); // Use Microsoft XDR

    xdr.open(method, url);

    xdr.onload = function () {
        var dom  = new ActiveXObject('Microsoft.XMLDOM'),
            JSON = $.parseJSON(xdr.responseText);

        dom.async = false;  // I've tried both true and false

        if (JSON == null || typeof (JSON) == 'undefined') {
            JSON = $.parseJSON(data.firstChild.textContent);
        }

        success(JSON);
    };

    xdr.onerror = function () {

        error();
    }

    xdr.send();

} 

// Do normal jQuery AJAX for everything else          
else {

    $.ajax({
        url: url,
        type: method,
        data: data,
        dataType: 'json',
        success: success,
        error: error,
        xhrFields: { withCredentials: true }
    });

}

}

RequestWrapper(

// URL
'http://myURL',

// Data
null, 

// error
function (xhr, status, error) { console.log('error: ' + status); },

// success
function (data) { console.log('success: ' + data); },

// method
'get'

);

EDIT: I tried using jsonp but get a parsererror. Also tried the iecors.js jQuery ajax custom transport (https://github.com/dkastner/jquery.iecors) .. still no dice

<script src="jquery.iecors.js"></script>
<script type="text/javascript">

function RequestWrapper (url, data, error, success, method) {

$.ajax({
    url: url,
    type: method,
    data: data,
    dataType: 'jsonp',
    success: success,
    error: error,
    xhrFields: { withCredentials: true }
});
}

RequestWrapper(

// URL
'http://givingimages.pixfizz.com/v1/users/1891548/books.json',

// Data
null, 

// error
function (xhr, status, error) { console.log('error: ' + status); },

// success
function (data) { console.log('success: ' + data); },

// method
'get'

);


</script>
1

There are 1 answers

2
user3090955 On

Try below code this is basic example its working fine all browsers already i test it.

<html>
<head>
<!-- Include jquery from Google here -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<script  type="text/javascript">
// Wait till dom is loaded
$(document).ready(function() {

 // Load ajax.php as JSON and assign to the data variable
 $.getJSON('ajax.php', function(data) {
    // set the html content of the id myThing to the value contained in data
    $("#myThing").html(data.value);
 });   
});
</script>
</head>
 <body>
  <p id="myThing"></p>
</body>
</html>

create ajax.php file

<?php
echo json_encode(array("value" => "Hello World"));
?>

i think this post helpful to you click here to view script