roodyszz0 2019-06-28
var _headerConfig = {}; if(url.indexOf('getcaptcha') !== -1) { _headerConfig = { Accept: 'image/png', responseType: 'arraybuffer', } } else if(url.indexOf('files/upload') !== -1) { _headerConfig = { 'Content-Type': 'multipart/form-data', responseType: 'json', } } else { _headerConfig = { 'Content-Type': 'application/json', Accept: 'application/json', responseType: 'json', } }
if("method" in options) { options.method = options.method.toUpperCase(); } else { options.method = "GET"; } if(options.method !== "GET") { if(!(options.params instanceof FormData)) { options.params = JSON.stringify(options.params); } }
this.xhr.open(options.method, url, true); for(var _i in _headerConfig) { if(_i === 'responseType') { this.xhr.responseType = _headerConfig[_i]; } else { this.xhr.setRequestHeader(_i, _headerConfig[_i]); } } if(token) { this.xhr.setRequestHeader("token", token); } this.xhr.send(options.params);
这个问题结合链式编程一度的卡了很久。
ajax.prototype.complete = function(completeFunction) { this.xhr.onreadystatechange = function(e) { if(this.readyState === 4) { completeFunction(this); } } return this; }
var requstUrl = { baseURL: URL, API: { NEWS: '/news', LOGIN: '/', }, // api为API中的参数,用于拼接url // method为API后的地址,用于拼接url最后面的东西。 // params为get请求需要的参数 createUrl: function(api, method, params) { var _requestUrl = this.baseURL + this.API[api] + method; if(params) { for(var i of params) { _requestUrl += (_requestUrl.indexOf("?") == -1 ? "?" : "&"); _requestUrl += name + "=" + value; } } return _requestUrl; } }
function handleRequest() { } // get请求带参数。 handleRequest.prototype.getDataUseGet = function(api, method, params) { var _url; var ajax = new Ajax(); var token = sessionStorage.getItem('token'); if(params) { _url = requstUrl.createUrl(api, method, params); } else { _url = requstUrl.createUrl(api, method); } return ajax.request(_url, { method: 'GET', params: params }, token); } // get请求不带token handleRequest.prototype.getDataUseGetWithoutToken = function(api, method, params) { var _url; var ajax = new Ajax(); if(params) { _url = requstUrl.createUrl(api, method, params); } else { _url = requstUrl.createUrl(api, method); } return ajax.request(_url, { method: 'GET', params: params }); } // post请求带token handleRequest.prototype.getDataUsePost = function(api, method, params) { var _url = requstUrl.createUrl(api, method); var token = sessionStorage.getItem('token'); var ajax = new Ajax(); console.log(createAjaxObj(_url, { method: 'POST', params: params }, token)); return ajax.request(_url, { method: 'POST', params: params }, token); } // post请求不带token handleRequest.prototype.getDataUsePostWithOutToken = function(api, method, params) { var _url = requstUrl.createUrl(api, method); var ajax = new Ajax(); return ajax.request(_url, { method: 'POST', params: params }); } // put请求带token handleRequest.prototype.getDataUsePut = function(api, method, params) { var _url = requstUrl.createUrl(api, method); var token = sessionStorage.getItem('token'); var ajax = new Ajax(); return ajax.request(_url, { method: 'PUT', params: params }, token); } // put请求不带token handleRequest.prototype.getDataUsePutWithOutToken = function(api, method, params) { var _url = requstUrl.createUrl(api, method); var ajax = new Ajax(); return ajax.request(_url, { method: 'PUT', params: params }); } // delete请求带token handleRequest.prototype.deleteData = function(api, method, params) { var _url = requstUrl.createUrl(api, method); var token = sessionStorage.getItem('token'); var ajax = new Ajax(); return ajax.request(_url, { method: 'DELETE', params: params }, token); }这个方法感觉可以再次进行封装。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> <script src="ip.js" type="text/javascript"></script> <script src="xhr.js" type="text/javascript"></script> <script src="request.js" type="text/javascript"></script> <script type="text/javascript"> var data = { "captcha": "string", "password": "string", "username": "string" }; var test = new handleRequest(); test.getDataUsePostWithOutToken('LOGIN', 'login',data).complete(function(result) { console.log(result) }) </script> </html>
成功发起请求。
完整代码点击查看
以上。
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
本文实例讲述了php+ ajax 实现的写入数据库操作。分享给大家供大家参考,具体如下:。<input class="tel" type="text" placeholder="请输入您的手机号码&q