原生js
ajax('get', 'http://127.0.0.1:3000/get', function(response){
alert(JSON.parse(response)['message']);
});
ajax('post', 'http://127.0.0.1:3000/post', function (response) {
alert(JSON.parse(response)['message']);
}, JSON.stringify({name: "post测试"}));
function ajax(method, url, callback, data, async){
var data=data || null;
var async=async || true;
var xhr = new window.XMLHttpRequest || ActiveXObject('Microsoft.XMLHTTP');
xhr.open(method, url, async);
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(data);
console.log("发送的数据是:"+data);
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status >=200&&xhr.status<300||xh