Ajax Post instances in JavaScript

Source: Internet
Author: User
Tags http post

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).

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.