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

前端 未结 4 974
萌比男神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:20

    Ran into the problem in a React Application

    Axios doesn't support JSONP they offers alternatives to perform jsonp requests:

    Follow this link to see some documentation for jsonp for axios: axios: https://github.com/mzabriskie/axios/blob/master/COOKBOOK.md#jsonp

    Here is my documentation:

    Step 1: $ npm install jsonp --save

    Step 2:

    import jsonp from 'jsonp';
    

    (this goes at the top of your component)

    Step 3: Create a method in your React Component:

    JSONP () {
            jsonp( "https://*YourUrlHere.jsonp", { name: 'Name Of JSONP Callback Function' }, (error, data) => {
                if (error) {
                    this.setState({
                        error,
                    });
                } else {
                    this.setState({
                        data: data,
                    });
                }
            });
        }
    

提交回复
热议问题