0

I'm making these ajax requests from a Google Chrome extension which I'm trying to create, but every time my extension makes a request it gets cancelled. I don't know why.

This snippet below works fine.

    function makeRequest(method, url){
         var xhr = new XMLHttpRequest();
         xhr.open("GET", "http://www.djjohal.com");
         xhr.send();
      }


      setTimeout(makeRequest, 10000)

But when I change the above code to this, it didn't work.

    var submit = document.querySelector('input[type=submit]')
    submit.addEventListener('click', callback)


    function makeRequest(method, url){
        var xhr = new XMLHttpRequest();
        xhr.open(method, url);
        xhr.send();
    }


    function callback(){
        makeRequest('GET', 'http://www.djjohal.com')
    }

What am I doing wrong?

soktinpk
  • 3,778
  • 2
  • 22
  • 33
Hybrid
  • 321
  • 3
  • 13

2 Answers2

2

Chrome Extensions are restricted from making cross-site requests unless the Extension has the correct security permission.

See Also: Cross-Origin XMLHttpRequest on the Chrome Developer site.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
1

This is due to a setting on the djjohal.com server.

Trying both snippets of code given in the question, I receive this message from Chrome in a normal webpage:

XMLHttpRequest cannot load http://www.djjohal.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

How to add an Access-Control-Allow-Origin header

If you aren't in control of djjohal, you aren't doing anything wrong.

Community
  • 1
  • 1
Devin H.
  • 1,065
  • 1
  • 11
  • 19