First, the XMLHttpRequest of the original ecology
| The code is as follows |
Copy Code |
<script language= "JavaScript" >
function Saveintroduce ()
{
Get the Accept return information layer
var introducemsg = document.getElementById ("introducemsg");
Get form objects and user information values
var f = Document.introduceedt;
var elm1 = encodeURIComponent (F.elm1.value);
Receive the URL address for the form
var url = "save.php?id=<?=$_get[" id "]?>";
The value of the post is required to join each variable by &
var poststr = ' elm1= ' + elm1;
Instantiating Ajax
var ajax = Initajax ();
var Ajax = false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest)
{//mozilla Browser
Ajax = new XMLHttpRequest ();
if (Ajax.overridemimetype)
{//Set MIME category
Ajax.overridemimetype ("Text/xml");
}
}
else if (window. ActiveXObject)
{//IE browser
Try
{
Ajax = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (E)
{
Try
{
Ajax = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!ajax)
{///exception, failed to create object instance
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}
To open a connection by post
Ajax.open ("POST", url, True);
Defines the transmitted file HTTP header information
Ajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Send post data
Ajax.send (POSTSTR);
Get Execution status
Ajax.onreadystatechange = function ()
{
If the execution status succeeds, the return information is written to the specified layer
if (ajax.readystate = = 4 && ajax.status = 200)
{
introducemsg.innerhtml = Ajax.responsetext;
}
}
}
</script>
<form id= "Introduceedt" name= "Introduceedt" action= "save.php>" method= "POST" >
<input name= "elm1" id= "elm1" ><input type= "button" value= "Submit Form" onclick= "Saveintroduce ()" ><div id= " Introducemsg "></div></div></td>
</form> |
Two, jquery Ajax.post
1.jQuery $.post () method
The $.post () method requests data from the server via an HTTP POST request.
Instance
| The code is as follows |
Copy Code |
|
$ ("button"). Click (function () {
$.post ("demo_test_post.asp",
{
Name: "Donald Duck",
City: "Duckburg"
},
function (data,status) {
Alert ("Data:" + Data + "Status:" + status);
});
}); |
2, the general format of $.ajax
| The code is as follows |
Copy Code |
|
$.ajax ({
Type: ' POST ',
Url:url,
Data:data,
Success:success,
Datatype:datatype
});
|
Ii. Description of $.ajax parameters
Parameter description
URL required. Specify which URL to send the request to.
Data is optional. The mapping or string value. Specify the data to be sent to the server along with the request.
Success (data, Textstatus, JQXHR) is optional. The callback function to execute when the request succeeds.
DataType Optional. Specify the data type of the expected server response.
The default execution of intelligent judgments (XML, JSON, script, or HTML).