zoukankan      html  css  js  c++  java
  • 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、完成
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
       

  • 相关阅读:
    pip install pli 提示:Could not find a version that satisfies the requirement PIL
    关于selenium部分元素定位不到的解决办法
    ERROR 1054 (42S22): Unknown column ‘password‘ in ‘field list‘
    通过Tomcat jpress连接不到数据库
    Navicat MySQL 连接出现 Authentication plugin ‘caching_sha2_password‘ cannot be loaded
    Selenium中核心属性以及方法
    selenium中定位frame中的元素
    selenium中截屏以及按照时间格式保存到相应文件夹
    Selenium中核心属性以及方法
    selenium中关于js脚本的一些操作
  • 原文地址:https://www.cnblogs.com/tl1025/p/5166761.html
Copyright © 2011-2022 走看看