0

I'm getting these errors and I'm not sure what I'm doing wrong. enter image description here

I'm new to making http requests using Javascript and I was wondering what I'm doing wrong in my request. If anyone can take a look and explain it to me, or point me to an already accepted answer, it would be greatly appreciated.

window.addEventListener("load", function(){
    let url = "https://api.guildwars2.com";
    let method = "GET";
    let async = true;
    let getData = "Get Data";
    let request = new XMLHttpRequest();
    request.onreadystatechange = function(){
        let status = request.readyState;
        let data = request.responseText;
        if(status == 4 && status == 200){
            console.log("Connection made");
        }
    }
    request.open(method, url, async);
    request.send();
    console.log(request);
})

Since this question has a very detailed answer already elsewhere, I'm closing it.

  • 4
    Possible duplicate of [Cross origin request blocked](http://stackoverflow.com/questions/24663126/cross-origin-request-blocked) – Bálint Jul 13 '16 at 11:12
  • @Bálint, so basically, I need to do an AJAX call? –  Jul 13 '16 at 11:13
  • 1
    Yeah, and if that still doesn't work, then put the `https://crossorigin.me/` address before the resource's address, this is basically a bridge to avoid crossorigin stuff. SO if your data is at `http://example.com/myData.file`, then you need to access `https://crossorigin.me/http://example.com/myData.file` – Bálint Jul 13 '16 at 11:15
  • @Bálint, that seems to have worked, except I'm running it on codepen and not a server. Thank you –  Jul 13 '16 at 11:17
  • Give me a sec, I give you my codepen snippet to load in resources – Bálint Jul 13 '16 at 11:18
  • 1
    http://pastebin.com/cpVEaDBB – Bálint Jul 13 '16 at 11:21

2 Answers2

0

This is because of CORS(Cross-Origin Resource Sharing), your are requesting different domain from your currently domain.

Read CORS Request for how to achieve this using CORS.

0

the last displayer error says that you are not allowed to retrieve data from another source ( CROSS ORIGIN REQUEST ) you'll have to configure your server to allow it.

I don't know what server you are using so here is a link to how to do it for many servers

Hicham Zouarhi
  • 1,030
  • 1
  • 18
  • 28