Details about ajax post instances in javascript

Source: Internet
Author: User

I. XMLHttpRequest

The Code is as follows:
<Script language = "javascript">

Function saveintroduce ()
{
// Obtain the accept response information layer
Var introducemsg = document. getElementById ("introducemsg ");
// Obtain the form object and user information value
Var f = document. introduceedt;
Var elm1 = encodeURIComponent (f. elm1.value );
// URL of the received form
Var url = "save. php? Id = <? = $ _ GET ["id"]?> ";
// The POST value. www. bKjia. c0m connects each variable through &.
Var postStr = 'elm1 = '+ elm1;
// Instantiate Ajax
// Var ajax = InitAjax ();
Var ajax = false;
// Start initializing the XMLHttpRequest object
If (window. XMLHttpRequest)
{// Mozilla Browser
Ajax = new XMLHttpRequest ();
If (ajax. overrideMimeType)
{// Set the 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. An error occurred while creating the object instance.
Window. alert ("the XMLHttpRequest object instance cannot be created .");
Return false;
}

// Open the connection through Post
Ajax. open ("POST", url, true );
// Define the HTTP header information of the transmitted File
Ajax. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
// Send POST data
Ajax. send (postStr );
// Obtain the execution status
Ajax. onreadystatechange = function ()
{
// If the execution status is successful, the returned 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> </td>

</Form>

Ii. jquery ajax. post

1. jQuery $. post () method

The $. post () method sends an http post request to request data from the server.

Instance

The Code is as follows:
$ ("Button"). click (function (){
$. Post ("demo_test_post.asp ",
{
Name: "Donald Duck ",
City: "Duckburg"
},
Function (data, status ){
Alert ("Data:" + data + "Status:" + status );
});
});
 


2. Common Format of $. ajax

The Code is as follows:
$. Ajax ({

Type: 'post ',

Url: url,

Data: data,

Success: success,

DataType: dataType

});

 
 

2. $. ajax parameter description

Parameter description
The url is required. Specifies the URL to which the request is sent.
Data is optional. Ing or string value. Specifies the data sent to the server together with the request.
Success (data, textStatus, jqXHR) is optional. The callback function executed when the request is successful.
DataType is optional. Specifies the expected server response data type.

Intelligent judgment (xml, json, script, or html) is performed by default ).

For more details, see http://www.bKjia. c0m/wy/jquery/57064.htm

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.