0

I can't access a URL using this script on IE9. In Chrome and on Firefox it works fine. When I debug (F12) on IE9, I receive:

SCRIPT5: ACCESS DENIED.

My function:

function NewPage2() {
    var xmlHttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlHttp = new XMLHttpRequest();

    } else { // code for IE6, IE5
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttp.open("GET", "https://graph.facebook.com/oauth/access_token?client_id=" +
        '<%=ConfigurationManager.AppSettings["clientId"].ToString() %>' + 
        '&redirect_uri=' + <%=ConfigurationManager.AppSettings["redirectUrl"].ToString() %>' +
        '&state=' + document.getElementById('text').value + 
        '&client_secret=' + '<%=ConfigurationManager.AppSettings["client_secret"].ToString() %>' +
        '&code=' + getUrlVars2()["code"], false);
    xmlHttp.send(null);
    end(xmlHttp.responseText + "&userId=" + getUrlVars2()["state"]);
}
ndsmyter
  • 6,535
  • 3
  • 22
  • 37
r-magalhaes
  • 427
  • 2
  • 9
  • 18

1 Answers1

1

This happens when you try to access an ajax request from a different domain to your main page. (In this case, you're accessing a URL from Facebook).

If you need to access a URL from a different domain, it is called a Cross Site Request. These are blocked by default because of the security implications, but it is still possible to do them with a bit more work.

It's pretty easy to do with jQuery.

The odd thing is that although you included jQuery in the tags on the question, your actual code isn't using jQuery at all -- in fact, the whole of the code you've provided would be a single line in jQuery, plus it would work with cross-site requests.

You need to use an Ajax technique call JSONP, which you can find documented on the jQuery site here: http://api.jquery.com/jQuery.ajax/

The code would look something like this:

$.ajax({
  dataType: 'jsonp',
  jsonp: 'jsonp_callback',
  url: 'https://graph.facebook.com/......',
  success: function () {
    // do stuff
  },
});

hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307