I'm trying to do a GET request on wikipedia API. Using jQuery as below works fine:
$.ajax({
url: '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=Test&callback=JSON_CALLBACK',
type: 'GET',
headers: {'X-Requested-With': 'XMLHttpRequest'},
crossDomain: true,
dataType: 'jsonp'
}).done(function(data) {
console.log("Data: ", data);
});
But I want to use fetch or axios api, which stops at pre-flight with request method: OPTIONS. Why it works in jQuery but not in the other APIs?
axios.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=Test&callback=JSON_CALLBACK',
{ headers: {'X-Requested-With': 'XMLHttpRequest',
'content-type': 'text/plain'}
})
.then(function (response) {
console.log("Response: ", response);
});
I saw that it might be related to the Content-Type of the GET request, on jQuery the default seems to be text/plain, however I didn't have success when trying to alter the content-type of fetch/axios requests which are being sent as text/html.
Any thoughts on what might be the problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…