1

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>
David Leonard
  • 21
  • 1
  • 3
  • possible duplicate of [JQuery ajax cross domain](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain) - note the jQuery.support.cors = true; – mplungjan Sep 13 '13 at 20:58
  • Why not use a regular ajax call with `jsonp` as datatype? – Johan Sep 13 '13 at 20:59
  • I'll try that .. sorry my experience with cross-domain is low. But seems everywhere I read ajax is not supported in IE8+9 and I have to use XDomain. – David Leonard Sep 14 '13 at 01:27
  • get a parsererror using jsonp datatype. Also tried the Ajax transport https://github.com/dkastner/jquery.iecors – David Leonard Sep 14 '13 at 04:12

1 Answers1

0

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

user3090955
  • 1
  • 1
  • 1
  • Unfortunately this won't work cross-domain. My requirements are cross-domain working in IE8 .. also should mention a cookie session needs to be established (which I can do with a same-domain login) however once I'm cross-domain where I need to make a bunch of API calls (GET) it breaks in IE8+9. My hunch is that cross-domain cookie sessions are not supported with XDomain but are with Ajax (not IE8) .. so may be up a creek without a paddle. – David Leonard Sep 14 '13 at 22:03
  • http://www.vaaah.com/php/view/JQuery/15/How-to-load-a-new-php-page-data-in-same-window-using-jQuery – user3090955 Nov 15 '13 at 11:24