1.封装ajax
var defaultUrl = 'http://192.168.1.1:3000/' function post_data(url,data,successfn,errorfn){ $.ajax({ url: defaultUrl+url, data: data, dataType: 'json', async: false, type: 'post', success: function(res) { successfn(res); }, error: function(err) { errorfn(err); } }); }; function get_data(url,data,successfn,errorfn){ $.ajax({ url: defaultUrl+url, data: data, dataType: 'json', async: false, type: 'get', success: function(res) { successfn(res); }, error: function(err) { errorfn(err); } }); } /** * get请求 * @param {Object} uri * @param {Object} data */ var getMan = function(uri, data) { return new Promise(function(resolve, reject) { $.ajax({ url:defaultUrl+ uri, data: data, type: 'get', timeout: 10000, headers: { "authorization": localStorage.getItem('qdht_manager_token') //此处放置请求到的用户token }, success: function(res) { resolve(res); }, error: function(error) { reject(error); } }); }); } /** * 单个POST请求公用 * @param {Object} uri * @param {Object} data */ var postMan = function(uri, data) { return new Promise(function(resolve, reject) { $.ajax({ url: defaultUrl+uri, data: data, type: 'post', timeout: 10000, headers: { "authorization": localStorage.getItem('qdht_manager_token') //此处放置请求到的用户token }, success: function(res) { resolve(res); }, error: function(error) { reject(error); } }); }); } //上传文件 post function uploadFile(uri, formData) { return new Promise(function (resolve, reject) { $.ajax({ url: uri, type: "POST", data: formData, processData: false, contentType: false, dataType: 'json', async: false, headers: { "authorization": localStorage.getItem('qdht_manager_token') //此处放置请求到的用户token }, success: function (res) { resolve(res); }, error: function (error) { reject(error); } }); }); }
2.获取上一页面传参的方法
function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for(var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); if(pair[0] == variable) { return pair[1]; } } return(false); } var id = getQueryVariable("id");