1

I am newbie to axios and I need to ask what is the different between using jQuery ajax and axios.

By jquery I often use like this:

d.init = function() {
        var urload = 'http://fashionablebloggertemplate.blogspot.com/feeds/posts/default?alt=json-in-script';
        $.when(
            $.ajax({
                type: 'POST',
                url: urload,
                context: this,
                contentType: "application/json",
                dataType: 'jsonp',
                success: function(data) {
                    //dataget here
                }
            });
        }

but in axios, I use like code below, it appears an error: No 'Access-Control-Allow-Origin' header is present on the requested resource while it is working well with jQuery ajax for different domain and local.

state = {
        dataposts: []
    }
    componentDidMount() {
      axios.get(`http://fashionablebloggertemplate.blogspot.com/feeds/posts/default?alt=json-in-script`)
        .then(res => {
          const dataposts = res.data;
          this.setState({ dataposts });
        })
    }

Can everyone explains for me and how to make it work?

Hai Tien
  • 2,929
  • 7
  • 36
  • 55
  • 1
    You can find similar discussion here for reference https://stackoverflow.com/questions/43471288/how-to-use-jsonp-on-fetch-axios-cross-site-requests – Diljohn5741 Oct 26 '18 at 08:34
  • Axios doesn't have any helper functions for making JSONP requests. If you want to use JSONP and don't want to use jQuery, then you'll need to make the request manually or find another library. Axios won't help you. – Quentin Oct 26 '18 at 08:40
  • @Quentin thank you. It is useful – Hai Tien Oct 26 '18 at 08:41
  • @Diljohn5741 thank you. It is useful – Hai Tien Oct 26 '18 at 08:41
  • 1
    @AJC24 — They aren't making a POST request with jQuery. It is set to `dataType: 'jsonp'` which forces it to GET despite what `type` says (`contentType` is also ignored). – Quentin Oct 26 '18 at 08:42

0 Answers0