Ajax Post examples in JavaScript

Source: Internet
Author: User
Tags http post

One, the XMLHttpRequest of the original ecology

Code to copy code as follows
<script language= "JavaScript" >

function Saveintroduce ()
{
Get Accept return information layer
var introducemsg = document.getElementById ("introducemsg");
Get Form object and user information values
var f = Document.introduceedt;
var elm1 = encodeURIComponent (F.elm1.value);
Receive the URL address of the form
var url = "save.php?id=<?=$_get[" id "]?>";
The value of post is required, www.111cn.net each variable is joined by &
var poststr = ' elm1= ' + elm1;
Instantiation of 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;
}

Open connection via 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 is successful, then write the return information 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 over an HTTP POST request.

Instance

Code to copy code as follows
$ ("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

Code to copy code as follows
$.ajax ({

Type: ' POST ',

Url:url,

Data:data,

Success:success,

Datatype:datatype

});


Second, the parameter description of $.ajax

Parameter description
URL required. Specifies which URL to send the request to.
Data is optional. The map or string value. Specifies the data that is sent to the server along with the request.
Success (data, Textstatus, JQXHR) is optional. The callback function to execute when the request succeeds.
DataType is optional. Specifies the data type of the expected server response.

The default is to perform smart judgments (XML, JSON, script, or HTML).

For more detailed information, please see: http://www.111cn.net/wy/jquery/57064.htm

Ajax Post examples in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.