$. Post Application Instance of ajax in jQuery

Source: Internet
Author: User

Post is a data submission mode in jquery ajax and is the same as the post method in the form. Let's take a look at a simple ajax post learning note.

Use jQuery to obtain AJAX asynchronous data. The following example shows the AJAX function similar to a user name.

JQuery. post (url, [data], [callback], [type]):Use POST for asynchronous requests


Parameters:

Url (String): the URL of the request.

Data (Map): (optional) data to be sent to the server, expressed in Key/value pairs.

Callback (Function): (optional) callback Function when the load is successful (this method is called only when the Response returns success ).

Type (String): (optional) the official description is: Type of data to be sent. In fact, it should be the client request type (JSON, XML, and so on)

This is a simple POST Request function to replace complex $. ajax. You can call the callback function when the request is successful. To execute a function when an error occurs, use $. ajax. Sample Code:


JS Code

The Code is as follows: Copy code

<Script>

$ (Document). ready (function (){

$ (". Ajax_btn"). click (function (){

$. Post ("ajax. php", // process dynamic pages Asynchronously

{Name: $ (". name"). val ()}, // gets the value of the text class named "name" and asynchronously transmits the value by NAME

Function (data) {// data is the reverse return value, and the function performs the reverse return value processing.

$ (". Content"). val (data); // enter the reversed value in the text box named "content ".

});

})

})

</Script>

Ajax. php code

The Code is as follows: Copy code

<? Php

$ Name = $ _ POST ["name"];

If ($ name = "netxu "){

Echo "sorry,". $ name. "data exists ";

}

Else {

Echo "congratulations,". $ name. "available ";

}

?>

HTML code

 

The Code is as follows: Copy code

<P> the page effect is as follows: (when you enter <a href = "http://www.hzhuti.com" title = "thoughts"> netxu </a>, a message is displayed, if you enter other information, the system prompts that the data is available.) </p>

<P> pass value text: <input type = "text" class = "name" size = "20" value = "netxu"/> </p>

<P> asynchronous reception: <input type = "text" class = "content" size = "20" style = "color: # F00 "/> <input type =" button "class =" ajax_btn "value =" Asynchronous processing "/> </p>


The same is true for ASP, but the output format is different.

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.