zoukankan      html  css  js  c++  java
  • $.ajax

    基于$.ajax声明一个简单的AjaxHelper构造器,AjaxHelper构造器的原型对象包含5个方法,分别用于处理GET, POST, PUT, DELETE和JSONP请求。

    function AjaxHelper() {
        this.ajax = function(url, type, dataType, data, callback) {
            $.ajax({
                url: url,
                type: type,
                dataType: dataType,
                data: data,
                success: callback,
                error: function(xhr, errorType, error) {
                    alert('Ajax request error, errorType: ' + errorType +  ', error: ' + error)
                }
            })
        }
    }
    AjaxHelper.prototype.get = function(url, data, callback) {
        this.ajax(url, 'GET', 'json', data, callback)
    }
    AjaxHelper.prototype.post = function(url, data, callback) {
        this.ajax(url, 'POST', 'json', data, callback)
    }
    
    AjaxHelper.prototype.put = function(url, data, callback) {
        this.ajax(url, 'PUT', 'json', data, callback)
    }
    
    AjaxHelper.prototype.delete = function(url, data, callback) {
        this.ajax(url, 'DELETE', 'json', data, callback)
    }
    
    AjaxHelper.prototype.jsonp = function(url, data, callback) {
        this.ajax(url, 'GET', 'jsonp', data, callback)
    }
    
    AjaxHelper.prototype.constructor = AjaxHelper

    发送get请求。实例:(vue实例)

    var ajaxHelper = new AjaxHelper()
    
    var demo = new Vue({
        el: '#app',
        data: {
            gridColumns: ['customerId', 'companyName', 'contactName', 'phone'],
            gridData: [],
            apiUrl: 'http://localhost:15341/api/Customers'
        },
        ready: function() {
            this.getCustomers()
        },
        methods: {
    
            getCustomers: function() {
                // 定义vm变量,让它指向this,this是当前的Vue实例
                var vm = this,
                    callback = function(data) {
                        // 由于函数的作用域,这里不能用this
                        vm.$set('gridData', data)
                    }
                ajaxHelper.get(vm.apiUrl, null, callback)
            }
        }
    })
    View Code

     --纯原生js代码片段:

    var ajax = function() {};
     ajax.prototype = {
         request: function(method, url, callback, postVars) {
             var xhr = this.createXhrObject();
             xhr.onreadystatechange = function() {
                 if (xhr.readyState !== 4) return;
                 (xhr.status === 200) ?
                     callback.success(xhr.responseText, xhr.responseXML) :
                     callback.failure(xhr,status);
             };
             if (method !== "POST") {
                 url += "?" + JSONStringify(postVars);
                 postVars = null;
             }
             xhr.open(method, url, true);
             xhr.send(postVars);
         },
         createXhrObject: function() {
             var methods = [
                 function() { return new XMLHttpRequest(); },
                 function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
                 function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
             ],
             i = 0,
             len = methods.length;
             for (; i < len; i++) {
                 try {
                     methods[i];
                 } catch(e) {
                     continue;
                 }
                 this.createXhrObject = methods[i];
                 return methods[i];
             }
             throw new Error("ajax created failure");
         },
         JSONStringify: function(obj) {
             return JSON.stringify(obj).replace(/"|{|}/g, "")
                         .replace(/b:b/g, "=")
                         .replace(/b,b/g, "&");
         }
     };
  • 相关阅读:
    关于线程池,那些你还不知道的事
    Java发送邮件
    原来实现项目多环境打包部署是如此的简单
    史上最全的maven的pom.xml文件详解
    Linux系统基础知识整理(一)
    计算机启动过程的简单介绍 计算机启动流程 计算机BIOS作用 POST 开机自检 计算机启动顺序 分区表 操作系统启动
    交换机工作原理、MAC地址表、路由器工作原理详解
    $(function(){})和$(document).ready(function(){}) 的区别
    关于RAM与ROM的区别与理解
    CDN的作用与基本过程
  • 原文地址:https://www.cnblogs.com/lanyueff/p/6645453.html
Copyright © 2011-2022 走看看