1

I have the following ajax call that is supposed to call a page on different domain:

if ($.browser.msie && window.XDomainRequest) {

            // Use Microsoft XDR
            var xdr = new XDomainRequest();
            xdr.open("post", "https://different-domain.aspx");
            xdr.onload = function() {
                alert(xdr.responseText);// XDomainRequest doesn't provide responseXml, so if you need it:                       

            };
            xdr.onerror = function() {
                alert("Error " + xdr.responseText);
            };
            xdr.onprogress = function() {
                alert('errored out');
            };

            var params = "fileName="+ file+"&param02="+ param02+"&param03="+ param03+"&param05="+ param05+"&param08="+ param08+"&param11="+ param11;
            alert(params);
            xdr.send(params);
        } 

The code section in the onerror method is executed, but the xdr.responseText is nothing - empty. Can anybody point to me what I am possibly doing wrong?

I am trying to get to call an ajax page on a different domain - one shortcut would be to change the Internet security setting to "Allow across different domains", but I do not want to tell my users to do that.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
oneiros
  • 3,527
  • 12
  • 44
  • 71

1 Answers1

1

I believe you're running into the Same-Origin-Policy, where you are not allowed to create AJAX requests to a different domain than the current one. There are ways around this; you can create a server-side script to use as a proxy or use JSONP if possible (it takes the remote server being properly setup for JSONP to work.

Since you tagged your question with jQuery I'll give you an example of how to do this inside the jQuery framework (which is one of the main reasons to use jQuery, it makes AJAX simple):

$.ajax({
    url      :  <url>,
    type     : 'post',
    dataType : 'jsonp',
    success  : function (serverResponse) {},
    error    : function (jqXHR, textStatus, errorThrown) {}
});

This uses JSONP to do your cross-domain request.

dataType : 'jsonp'

Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Docs for $.ajax():

Source: http://api.jquery.com/jquery.ajax

There are many more options you can set while making an AJAX request via jQuery, check-out the documentation to see them all.

Jasper
  • 75,717
  • 14
  • 151
  • 146
  • It does not work in IE8. What I am trying to do here is to circumvent the "Allow access accross domains" option of IE8 being disabled. For that I need a cross domain request. Setting the datatype to jsonp, does not do the deal in IE8. At least not for me. Any suggestions Jasper? – oneiros Feb 21 '12 at 22:18
  • I would suggest to use a proxy server-side script. So you can target the server-side script that is on the same domain as the rest of your code and it does the cross-domain work. Flow would be: JS does AJAX call to server-side script, server-side script does cross-domain request, server-side script then outputs the return from it's request which is in-turn read by the JS. – Jasper Feb 21 '12 at 22:30
  • Yeah. Could do that. How would that JSP page/Servlet do a post to the page I have in the code with those parameters? – oneiros Feb 21 '12 at 22:32
  • I'm not familiar with JSP, but I do this with ASP Classic, PHP, and Cold Fusion, so I'll assume it's possible to do in JSP. This article looked promising: http://www.devx.com/java/Article/17679/1954 – Jasper Feb 22 '12 at 00:11