Suresh Atta
Suresh Atta

Reputation: 121998

Making get request for same domain giving CORS error

In a browser extension, this my ajax call

var xhr = new XMLHttpRequest();
xhr.open('GET', window.location.href, true);
xhr.responseType = "arraybuffer";
xhr.onload = function(event) {
  alert(this.response);
};

I really do not understand why this giving me the error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

CORS comes into picture only once we are making the calls to other domain. But here I am making the call to the same domain. You can witness that in url xhr.open('GET', window.location.href, true);

What I am missing here ?

enter image description here

Upvotes: 2

Views: 1798

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You can use a web service such as CrossOrigin.me to get ahead of it. I used a code like this and it did work for me:

$.ajax({
  url: 'https://crossorigin.me/http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf',
  jsonpCallback: 'callback',
  type: 'GET',
  success: function (data) {
    console.log(data);
  }
});

Fiddle: http://jsfiddle.net/L84u10yj/

I was able to make it working here:

Preview

Upvotes: 1

Related Questions