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("用户名密码错误!");

         }

    })

    })

  • 相关阅读:
    android操作数据库
    Android读写SD卡上的文件
    第四章 函数与程序结构
    getchar()与EOF
    NULL, '',0 '0'的区别
    TCPL 行计数
    行计数
    getchar()用法
    在C语言中,double、long、unsigned、int、char类型数据所占字节数
    队列——解密QQ号
  • 原文地址:https://www.cnblogs.com/shenbing/p/5375328.html
Copyright © 2011-2022 走看看