function doGet(url, success, error, async) { //有bug! 默认是异步; if(async === '' || async === null || async === undefined){ async = true; } var token=localStorage.getItem("token"); var headers={ // "Content-Type":"application/json", 'X-AUTH-TOKEN':localStorage.getItem("token"), "Access-Control-Allow-Origin":"*", }; if (!token){ headers={ "Access-Control-Allow-Origin":"*" }; } $.ajax({ type:"GET", headers: headers, dataType:"json", url: baseUrl + url, async: async, success:success, error:error }); }
function doPost(url, data, success, error) { $.ajax({ type: "POST", headers: { // "Content-Type":"application/json",// 向后台传参报错 注释掉 'X-AUTH-TOKEN':localStorage.getItem("token"), "Access-Control-Allow-Origin":"*", }, dataType:"json", url: baseUrl + url, async:false, data : data, success : success, error : error }); }
调用方式
doPost("url", { name:name }, function(success){ }, function(error){ } );