How to use JSONP on fetch/axios cross-site requests

前端 未结 4 977
萌比男神i
萌比男神i 2020-12-15 06:55

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&acti         


        
4条回答
  •  眼角桃花
    2020-12-15 07:16

    Recommended way to access JSONP with axios is actually to not use axios:

    1. The Discussion on Why (not)
    2. A similar question

    3. Alternative Suggestion from Axios

    In short install:

    npm install jsonp --save
    

    use it like:

    var jsonp = require('jsonp');
    
    jsonp('http://www.example.com/foo', null, function (err, data) {
      if (err) {
        console.error(err.message);
      } else {
        console.log(data);
      }
    });
    

提交回复
热议问题