Ajax-GET, POST method of transmitting data and receiving return data

First, create an Ajax object

function ajaxFunction(){
var xmlHttp;
try{ // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e){
try{// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}

xmlHttp return;
}
---------------------
the GET method

GET function () {
// get the object
var = ajaxFunction Request ();
// transfer data to one of your Servlet
request.open ( "GET", "? / myAjax / Demo01 name = CZC & Age = 22", to true);

// return the received data and transmits only can not write
request.onreadystatechange = function () {
IF (request.readyState == == 200 is request.status &&. 4) {
Alert (request.responseText);
}
}

// send data
request.send ();
}
---------------------
the POST method

POST function () {
// get the object
var request = ajaxFunction ();
// address sent by
the request.open ( "the POST", "/ myAjax / Demo01", to true);
// receive data back
request.onreadystatechange = function ()
{
IF (request.readyState == == 200 is request.status &&. 4)
{
Alert (request.responseText);
}
}
// set the header
request.setRequestHeader ( "Content-type", "application / x-www- urlencoded-form ");
// send the content data
request.send (" name = cz & age = 22 ");

}
---------------------

Original link: https: //blog.csdn.net/weixin_40007271/article/details/87869071

Guess you like

Origin www.cnblogs.com/rickdiculous/p/11368735.html