谁学到邹老师的ajax的公共组件编写了,这块我怎么也搞不懂,跟了几遍代码也无果,望高手指点一二
尤其是后面添加的原型方法- function AjaxRequest(method, url, data, callback) {
- this.method = method;
- this.url = url;
- this.data = data;
- this.callback = callback;
- this.send = function () {
- //1.创建异步对象
- var xhr = createXmlHttp();
- //2.设置请求参数
- this.url = (this.data.length > 0 && this.method == "get") ? this.url + "?" + this.data : this.url;
- xhr.open(this.method, this.url, true);
- //3.根据请求方式 设置 响应的请求状态行
- if (this.method == "get") {
- xhr.setRequestHeader("If-Modified-Since", 0);
- }
- else {
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- }
- //4.设置回调函数
- xhr.onreadystatechange = this.bind(this,function(){
- if (xhr.readyState == 4 && xhr.status == 200) {
- //获得 服务器响应的json数组字符串
- this.callback(xhr.responseText);
- }
- });
- //5.发送请求报文
- if (this.method == "get") {
- xhr.send(null);
- }
- else {
- xhr.send(this.data);
- }
- };
- }
- //为AjaxRequest添加原型方法
- AjaxRequest.prototype = {
- bind: function (object, fun) { return function () { return fun.apply(object, arguments); } }
- }
复制代码 |