• Ajax学习


    // 想发送HTTP请求,必须先初始化XMLHTTP对象
    var httpRequest=new ActiveXObject("Microsoft.XMLHTTP");

    //更加完善的创建XMLHTTP请求的JavaScript代码
    function CreateRequest(){
        var httpRequest;
        if(window.XMLHTTPRequest){//Safari、Firefox、Chrome
            httpRequest=new XMLHTTPRequest();
        }
        else if(window.ActiveXObject){//IE
            httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
        }
        return httpRequest;
        }
        
        //发送Http请求
        var httpRequest=CreateRequest();
        httpRequest.open('GET','~/Login.ashx',true);
        httpReques.send(null);
        
        //Ajax open(method,url,asynchronous)
        //method  发送的HTTP方法必须大写
        //url 安全限制,必须请求本域名下的url
        //asynchronous 是否异步,false阻止Js发送的请求
        
        //send(data)
        //当发送的请求为POST时,发送的请求格式为(username=admin&password=123456)
        
        //示例,GET请求
        var httpRequest=CreateRequest();
        httpRequest.open('GET','~/Login.ashx?username=admin&password=123456',true);
        httpRequest.send(null);
        
        //请求的消息
        GET/~/Login.ashx?username=admin&password=123456 HTTP/1.1
        
        //POST请求   发送POST请求时必须修改发送的HTTP消息头,否则发送的HTTP消息不会被服务器接收
        
        <input type="button" value="向服务器发送POST请求……" onclick="AjaxPost()" />
        function AjaxPost(){
            var httpRequest=CreateRequest();
            httpRequest.open('POST','request.ashx',true);
            httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencode');
            httpRequest.send('username=admin&password=123456');
        }
        
        //响应回调函数
        var req=CreateRequest();
        req.onreadystatechange=function(){
        };//匿名回调函数
        //在回调函数中我们首先要检查回调函数的状态
        req.onreadystatechange=function(){
            if(req.readystate==4){
                alert('AjaxRequest is OK!');
            }
            else{
                alert('AjaxRequest never complete!');
            }
        };
        //请求状态包括五种:
        //0、未初始化,没有调用open方法
        //1、已经调用send的方法,正在发送请求
        //2、send发送结束,已经接收到全部HTTP响应消息
        //3、正在解析请求消息,消息状态和响应头不可用
        //4、完成
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
       

  • 相关阅读:
    笔记44 Hibernate快速入门(一)
    tomcat 启用https协议
    笔记43 Spring Security简介
    笔记43 Spring Web Flow——订购披萨应用详解
    笔记42 Spring Web Flow——Demo(2)
    笔记41 Spring Web Flow——Demo
    Perfect Squares
    Factorial Trailing Zeroes
    Excel Sheet Column Title
    Excel Sheet Column Number
  • 原文地址:https://www.cnblogs.com/tl1025/p/5166761.html
Copyright © 2020-2023  润新知