Home > Web Front-end > JS Tutorial > How to use ajax to create an XMLHttpRequest object_Basic knowledge

How to use ajax to create an XMLHttpRequest object_Basic knowledge

WBOY
Release: 2016-05-16 17:47:03
Original
1027 people have browsed it

Does it have to be this complicated every time I create an object? The following code:
JScript code:
"testAjax.htm" File:

Copy code The code is as follows:



try
{
// Firefox, Opera 8.0, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch (e)
 {
 alert("Your browser does not support AJAX!");
 }
 }
 }
 }
 
 

  User:
Time:
 

 


First declare an xmlHttp variable that holds the XMLHttpRequest object.
Then use XMLHttp=new XMLHttpRequest() to create this object. This statement is specific to Firefox, Opera, and Safari browsers. If that fails, try xmlHttp=new ActiveXObject("Msxml2.XMLHTTP") for Internet Explorer 6.0, and if that also fails, try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") for Internet Explorer 5.5.

If none of these three methods work, the user is using an outdated browser and he or she will see a prompt stating that the browser does not support AJAX.

You don’t have to go through so much trouble. You can directly save the definition of this function as a separate js file and reference this file in the page that needs to use AJAX.
As detailed below:
JScript code:




Copy code


The code is as follows:

function CreateHTTPObject() { var xmlhttp; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e )
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest)
{
try
{
xmlhttp = window.createRequest();
}
catch (e)
{
xmlhttp=false;
}
}
return xmlhttp;


} Define the above function and create an instance when calling it, as follows:
JScript code:




Copy code

The code is as follows:

var xmlHttp = CreateHTTPObject(); if (!xmlHttp) { return; //Unable to create xmlhttp object} xmlHttp.open("GET", url, true); xmlHttp.onreadystatechange = function(){HandleRequest(xmlHttp, "Element ID")};
xmlHttp.send(null);


You can also use jquery directly to do it in one sentence, as follows:




Copy the code

The code is as follows:

$(document).ready(function(){
$("#userpass").blur(function(){
var password=$("#userpass").val( );
var name=$("#username").val();
if(password==""||password==null){
$("#pass").html ("Please enter your password! ");
b=false;
}else if(!/^[a-zA-Z0-9_] {6,16}$/.test(password)){
$("#pass").html("The input format is incorrect! The password should be at least 6 digits or Characters");
b=false;
}else{
$.get("LoginAjaxPassword",{"userpass":encodeURI(encodeURI(password)),"username": encodeURI(encodeURI(name))},function(response){
$("#pass").html(response);
if(response=="" "√" ""){
b=true;
}
});
}
return b;
});
$("#login-submit").click(function(){
var autologin=document.getElementById("autologin").checked;
if(a&&b){
//if ($("#autologin").attr("checked")==true){
if(autologin==true){
//${"#login-user-form"}.attr( "action","AutoLogin");
//$("#login-user-form").submit();
document.form.action="AutoLogin";
document.form. submit();
}else{
//${"#login-user-form"}.attr("action","Login");
//$("#login-user -form").submit();
document.form.action="Login";
document.form.submit();
}
} else{}
});
});

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template