当前位置:首页>正文

js的ajax代码,不用任何框架,就用js怎么实现ajax ajax登录跳转如何实现的

2023-07-05 19:51:02 互联网 未知

js的ajax代码,不用任何框架,就用js怎么实现ajax?

var xmlhttp
if (window.XMLHttpRequest) 
    xmlhttp=new XMLHttpRequest() 
else 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
//创建xmlhttp对象
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
        // 成功后执行的代码
        //服务器返回数据为xmlhttp.responseText
     }

}
xmlhttp.open(方法,url,是否异步)
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded")
xmlhttp.send(data)
//get时data为null

ajax登录跳转如何实现的?

ajax跳转方法:

else {
            $.ajax({
                type: "post",
                url: "Login.ashx",
                async: false,
                data: { UserName: $("input[name=txtuser]").val(),
                    PWD: $("input[name=txtpwd]").val(),
                    Code: $("input[name=validation]").val()
                },
                success: function (data, status) {
                    var types = data
                    if (types.toString() != "登录成功") {
                        alert(data)
                    }
                    else{
                    window.location.href="main.aspx"
                    }
                },
                error: function () { alert("用户名密码验证失败") }

            })
return false
        }