A detailed explanation of the use of the JQuery Ajax Post ()

Source: Internet
Author: User
Tags http post json php code

First know to Jquery.post (URL, [data], [callback], [type])

Describe the parameters:

URL: Sends the request address.

Data: Key/value parameter to be sent.

Callback: callback function when sending success.

Type: Returns the content format, XML, HTML, script, JSON, text, _default.

Description

Loads information through a remote HTTP POST request.

This is a simple POST request function to replace the complex $.ajax. Callback functions can be invoked when the request succeeds. If you need to perform a function when an error occurs, use $.ajax.

Let's take a look at a simple example

The code is as follows Copy Code

<?php echo Json_encode (Array ("name" =>$_post[' name '));? >

Then set up ajax.html file, note JS code: www.111cn.net

The code is as follows Copy Code


<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<script type= "Text/javascript" src= "/jquery-1.7.1.min.js" ></script>
<script>
$ (document). Ready (function () {
$ ("#sub"). Click (function () {
$.post ("testpost.php", {name:$ ("#name"). Val ()},function (data,textstatus) {
$ ("#result"). Append ("Data:" +data.name);
$ ("#result"). Append ("<br>textstatus:" +textstatus);
}, "JSON");
return false;
});
});
</script>
<body>
<form action= "testpost.php" method= "POST" >
<input type= "text" name= "name" id= "Name" >
<input type= "Submit" id= "sub" value= "submitted" >
</form>
The contents of <div id= "Result" ></div>
</body>

Usage 2: (click Post data to return data)

The code is as follows Copy Code

<input type= "button" id= "Bnajax" value= "Ajax" onclick= "Ajaxtest ()"/>

<script type= "Text/javascript" >

function Ajaxtest ()
{
$.post ("http://localhost:8012/t.asp", {"TXT": "123"},function (data)
{
$ ("#divMsg"). HTML (data);
}
);
}

</script>

Example 3

JS Code:

  code is as follows copy code

<script

$ (document). Ready (function () {

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

      $.post ("ajax.php",//Asynchronous Processing dynamic page

     {name:$ (". Name"). Val ()},//get the class named " The value of name literal, the value of

     function (data) {//data to return value, function to reverse value,

            $ (". Content"). val (data); When you get a return value, fill it in a text box with the class name "content"

     });

   })

})

</script>

 


ajax.php Code:

The code is as follows Copy Code

<?php

$name =$_post["name"];

if ($name = = "Netxu") {

echo "I'm sorry," $name. " Data exists ";

}

else{

echo "Congratulations," $name. " can use ";

}

?>

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.