为什么AJAX请求适用于jQuery但不适用于Fetch()或Axios

时间:2017-01-21 15:05:37

标签: jquery ecmascript-6 fetch axios

我正在研究需要AJAX调用的东西。我想使用新的ES6 API进行编码,但它不适用于POST个请求。我尝试使用jQuery,它按预期工作。我不知道为什么fetch()或Axios lib不起作用并发送POST参数

//------------------------------------------------------------------------
// Handler: ES6 Fetch API
// Method:  GET Request
//------------------------------------------------------------------------
fetch('http://127.0.0.1/playground/server.php?handler=Fetch&method=GET')
    .then( response => response.json() )
    .then( data => console.log(data) )
    .catch( (e) => console.log(e) );



//------------------------------------------------------------------------
// Handler: ES6 Fetch API
// Method:  POST Request
//------------------------------------------------------------------------
fetch('http://127.0.0.1/playground/server.php', {
    method: 'post',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        handler: 'Fetch',
        method: 'POST'
    })
})
    .then( response => response.json() )
    .then( data => console.log(data) )
    .catch( (e) => console.log(e) );



//------------------------------------------------------------------------
// Handler: jQuery
// Method:  GET Request
//------------------------------------------------------------------------
import $ from 'jquery';

$(document).ready( () => {
    $.get('http://127.0.0.1/playground/server.php', {
            handler: 'jquery',
            method: 'GET'
        }, (data) => console.log(data) );
} );



//------------------------------------------------------------------------
// Handler: jQuery
// Method:  POST Request
//------------------------------------------------------------------------
$(document).ready( () => {
    $.post('http://127.0.0.1/playground/server.php', {
        handler: 'jquery',
        method: 'POST'
    }, (data) => console.log(data) );
} );



//------------------------------------------------------------------------
// Handler: Axios
// Method:  GET Request
//------------------------------------------------------------------------
import Axios from 'axios';

Axios.get('http://127.0.0.1/playground/server.php', {
    params: {
        handler: 'Axios',
        method: 'GET'
    }
})
    .then( (r) => console.log(r) )
    .catch( (err) => console.log(err) );



//------------------------------------------------------------------------
// Handler: Axios
// Method:  POST Request
//------------------------------------------------------------------------
Axios.post('http://127.0.0.1/playground/server.php', {
    handler: 'Axios',
    method: 'POST'
})
    .then( (r) => console.log(r) )
    .catch( (err) => console.log(err) );

0 个答案:

没有答案
相关问题