Unable to fetch data from mediawiki api using XMLHttpRequest

Unable to fetch data from mediawiki api using XMLHttpRequest
0

#1

Tell us what’s happening:

request always gets cancelled

Your code so far

submit.onclick = function(){
   var xhr = new XMLHttpRequest();
  xhr.open('GET','https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=apple',true);
  xhr.setRequestHeader( 'Api-User-Agent', '[email protected]' );
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 &&  xhr.status == 200){
       var response = JSON.parse(xhr.responseText);
        alert(response);
    }
  };
  xhr.send();
};

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36.

Link to the challenge:


#2

Add origin=* to the request as shown here


#3

still the request is getting canceled after adding origin = * in the url
xhr status is cancelled


#6

Thanks SkyC it worked