2017-04-18 87 views
2

我试图在wikipedia API上执行GET请求。使用jQuery如下工作正常:如何在fetch/axios跨站点请求上使用JSONP

$.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); 
}); 

但我想用提取或爱可信API,它在飞行前停止与请求方法:选项。为什么它在jQuery中工作,但不在其他API中?

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); 
    }); 

我发现它可能与GET请求的内容类型,jQuery的默认似乎是text/plain的,但试图改变内容时,我没有成功 - 正在发送为text/html的提取/ axios请求类型。

有什么想法可能是什么问题?

+0

我不认为axios支持jsonp。 – noahnu

+0

你说得对,我会解决这个问题 –

回答