zoukankan      html  css  js  c++  java
  • javascript

    原生js实现ajax。创建xhr对象


    var xmlHttp;
    function createxmlHttpRequest() {
    if (window.ActiveXObject) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
    xmlHttp=new XMLHttpRequest();
    }

    get方法:


    function doGet(url){
    // 注意在传参数值的时候最好使用encodeURI处理一下,以防出现乱码
    createxmlHttpRequest();
    xmlHttp.open("GET",url);
    xmlHttp.send(null);
    xmlHttp.onreadystatechange = function() {
    if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) {
    alert('success');
    } else {
    alert('fail');
    }
    }
    }

    post方法:


    function doPost(url,data){
    // 注意在传参数值的时候最好使用encodeURI处理一下,以防出现乱码
    createxmlHttpRequest();
    xmlHttp.open("POST",url);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(data);
    xmlHttp.onreadystatechange = function() {
    if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) {
    alert('success');
    } else {
    alert('fail');
    }
    }
    }

  • 相关阅读:
    EFCore
    PS-邮件发送异常信息
    python-Django
    Autofac
    swagger
    查看哪个程序占用了端口
    SQL SERVER-系统数据库还原
    破解root密码
    WebApi路由
    async,await.task
  • 原文地址:https://www.cnblogs.com/jackzzx/p/4638223.html
Copyright © 2011-2022 走看看