zoukankan      html  css  js  c++  java
  • javaScript AJAX

    AJAX的实现

    var sAjax = function () {
            var sendMsg = {                                             
                url: "",
                sendType: "POST",
                ContentType: "application/x-www-form-urlencoded",
                msgType: "JSON",
                data: {},
                success: function (data) {
    
                },
                error: function (data) {
    
                },
                send: function () {
                    var url = "";
                    if (sendMsg.sendType == "POST") {
                        url = sendMsg.url;
                    } else {
                        url = sendMsg.url + "?" + postData(sendData.data);
                    }
                    var xhr = new XMLHttpRequest();
                    xhr.open(sendMsg.sendType, url, true);
                    xhr.setRequestHeader("Content-type", sendMsg.ContentType);
                    xhr.onreadystatechange = function () {
                        var XMLHttpReq = xhr;
                        if (XMLHttpReq.readyState == 4) {
                            if (XMLHttpReq.status == 200) {
                                var text = XMLHttpReq.responseText;
                                if (sendMsg.msgType == "JSON") {
                                    console.log(text);
                                    sendMsg.success(eval('(' + text + ")"));
                                } else {
                                    sendMsg.success(text);
                                }
                            } else {
                                sendMsg.error(XMLHttpReq.status);
                            }
                        }
                    };
                    xhr.send(postData(sendMsg.data));
                }
            }
            function postData(obj) { // 转成post需要的字符串.
                var str = "";
                for (var prop in obj) {
                    str += prop + "=" + obj[prop] + "&"
                }
                return str;
            };
            return sendMsg;
        }
    

    注意1:
    构造函数中的sendMsg内的send方法中不可使用this.的方式获取sendMsg对象中的属性,因为this是静态的,当使用var a =new sAjax()实例化对象时this就已经生成了,这个时候this中的属性还没有被赋值this.url=null。
    注意2:
    使用eval()函数转换json字符串时应当使用sendMsg.success(eval('(' + text + ")"))的方式进行解析,使用sendMsg.success(eval( text))会报错提示“VM444:1 Uncaught SyntaxError: Unexpected token …)”,原因是在javaScript中“{}”表示一个对象,而对象不能脱离变量单独存在,“()”在javascript中有2种作用:确立运算优先级,以及分组运算符。在这里“()”起到了分组运算符的作用。
    分组运算符:
    Return the result of evaluating Expression. This may be of type Reference.
    百度翻译
    返回评价结果。这可能是类型参考。
    基本没看懂,但是比较eval()函数的说明,本人理解为,eval('{"aaa":"aaa"}') 是将 '{"aaa":"aaa"}' 字符串当做语句来执行,执行这个语句在语法上是错误的,而eval('(' + '{"aaa":"aaa"}' + ")")是返回了'{"aaa":"aaa"}'的评价结果。就像是在函数中return一个变量一样。

  • 相关阅读:
    嵌套循环
    for循环
    while循环
    switch多选择结构
    python9--内存管理 引用计数 标记清除 分代回收
    python8--文件操作 with。。。open语法
    python7 数据类型的相互转化 字符编码
    python6-深浅拷贝 元组类型 字典类型 集合类型
    python5 数字类型 字符串类型 列表类型
    python4 分支结构,循环结构 for循环
  • 原文地址:https://www.cnblogs.com/shiqing/p/6229138.html
Copyright © 2011-2022 走看看