我正在研究需要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) );