小编Lua*_*ler的帖子

如何在fetch/axios跨站点请求上使用JSONP

我正在尝试在维基百科API上执行GET请求.使用jQuery如下工作正常:

$.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);  
});
Run Code Online (Sandbox Code Playgroud)

但是我想使用fetch或axios api,它在飞行前通过请求方法停止:OPTIONS.为什么它适用于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);  
    });
Run Code Online (Sandbox Code Playgroud)

我看到它可能与GET请求的Content-Type有关,在jQuery上默认似乎是text/plain,但是在尝试改变fetch/axios请求的内容类型时我没有成功以text/html发送.

关于可能出现什么问题的任何想法?

ajax jquery fetch axios

8
推荐指数
3
解决办法
2万
查看次数

标签 统计

ajax ×1

axios ×1

fetch ×1

jquery ×1