zoukankan      html  css  js  c++  java
  • js jq封装ajax方法

    json文本格式

    {

    "userInfo":[

                {name:"admin",password:"123"},

                {name:"admin1",password:"123"}

        ]

    }

    js:

    window.onload=function(){

    var txtName=..;

    var txtPwd=..;

    var url="Login.aspx?name="+txtName.value+"&pwd="+txtPwd.value:

    //调用封装后的异步方法

    myAjax("get",url,function(data){

    if(data=="OK"){

        window.location.href="main.aspx";

    }else{

     alert(data);

    }

    });

    }

    //封装后的方法

    function myAjax(httpMethod,url,callback){

    var xhr;

    if(XMLHttpRequest){

        xhr=new XMLHttpRequest();

    }else{

        xhr=new ActionXObject("Microsoft.XMLHTTP");

    }

      xhr.open(httpMethod,url,true);

      xhr.open();

      xhr.onreadstatechange=function(){

          if(xhr.readState==4&&xhr.status==200){

              callback(xhr.responeText);

           }

    }

    }

    jq:

    $("btn").click(function(){

       var txtName=..;

       var txtPwd=..;

    $.get("Login.ashx",{name:txtName,pwd:txtPwd},function(data){

          if(data=="OK"){

            window.location.href="main.aspx";

         }else{

            alert("用户名密码错误!");

         }

    })

    })

  • 相关阅读:
    魔术球问题
    【模板】网络最大流
    [SCOI2010]股票交易
    [SCOI2009]生日礼物
    [HAOI2007]修筑绿化带
    [HAOI2007]理想的正方形
    [USACO12MAR]花盆Flowerpot
    滑动窗口
    斐波那契公约数
    [SDOI2008]仪仗队
  • 原文地址:https://www.cnblogs.com/shenbing/p/5375328.html
Copyright © 2011-2022 走看看