Based on Jquery.ajax post () usage detailed

Source: Internet
Author: User

$.post (Url,[data],[callback],[type])

Description: This function is similar to the $.get () parameter, more than a type parameter, type is the requested data type, can be html,xml,json and other types, if we set this parameter to: JSON, then the return format is JSON format, if not set, and $. Get () is returned in the same format as a string of

The code is as follows Copy Code

$.post ("data.php", $ ("#firstName. Val ()"), function (data) {

$ ("#postResponse"). HTML (data.name);

"JSON"//Set the type of get data, so the data format is JSON type

);

The default execution of intelligent judgments (XML, JSON, script, or HTML).

Full HTML code:

  code is as follows copy code
<script type= "Text/javascript" src= "/jquery/jquery.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("input"). KeyUp (function () {
txt=$ ("Input"). Val ();
$.post ("/jquery/gethint.asp", {suggest:txt},function (result) {
$ ("span"). HTML (result);
});
});
});
</script>
<body>
<p> Please type a name (A to Z character) in the input box below:</p>
Name: <input type= "text"/>
<p> Suggestions:<span></span></p>
<p> Note: We explained the file (_blank) used in this example in <a href= "/ajax/ajax_asp_php.asp" target= ">ajax" gethint.asp tutorial </a>. </p>
</body>

PHP File:

The code is as follows Copy Code


<?php
To populate an array with a name
$a []= "Anna";
$a []= "Brittany";
$a []= "Cinderella";
$a []= "Diana";
$a []= "Eva";
$a []= "Fiona";
$a []= "Gunda";
$a []= "Hege";
$a []= "Inga";
$a []= "Johanna";
$a []= "Kitty";
$a []= "Linda";
$a []= "Nina";
$a []= "Ophelia";
$a []= "Petunia";
$a []= "Amanda";
$a []= "Raquel";
$a []= "Cindy";
$a []= "Doris";
$a []= "Eve";
$a []= "Evita";
$a []= "Sunniva";
$a []= "Tove";
$a []= "Unni";
$a []= "Violet";
$a []= "Liza";
$a []= "Elizabeth";
$a []= "Ellen";
$a []= "Wenche";
$a []= "Vicky";

Get the Q parameter from the URL
$q =$_get["Q"];

If q is greater than 0, find all the hints in the array
if (strlen ($q) > 0)
{
$hint = "";
for ($i =0; $i <count ($a); $i + +)
{
if (Strtolower ($q) ==strtolower (substr ($a [$i],0,strlen ($q)))
{
if ($hint = = "")
{
$hint = $a [$i];
}
Else
{
$hint = $hint. ",". $a [$i];
}
}
}
}

If no hint is found, set the output to "no suggestion"
Otherwise set to the correct value
if ($hint = = "")
{
$response = "no suggestion";
}
Else
{
$response = $hint;
}

Output response
Echo $response;
?>


This example should be full of online

Study notes reproduced
index.html file

The code is as follows Copy Code

<title>jquery Ajax Example Demo </title>
<script src= "./js/jquery.js" type= "Text/java script" ></script>
<script type= "Text/java script" >
$ (document). Ready (function () {//This is Jqueryready, it's like the C language main all operations contained inside it
$ ("#button_login"). MouseDown (function () {
Login (); Click the button with ID "Button_login" to trigger the function login ();
});
});
function login () {//functions login ();
var username = $ ("#username"). Val ();//The user name in the Fetch box
var password = $ ("#password"). Val ();//password in the Fetch box
$.ajax ({//an AJAX process
Type: "POST",//post to communicate with the background
URL: "login.php",//Communication with this PHP page
DataType: ' json ',//return value from PHP interpreted in JSON
Data: ' username= ' +username+ ' &password= ' +password,//php to be sent to two items, the above is the U and P
Success:function (JSON) {//If PHP is invoked successfully

Alert (json.username+ ' n ' +json.password); Send the return value (json.username) in PHP to alert

$ (' #result '). HTML ("Name:" + json.username + "<br/> Password:" + Json.password); Display the return value in PHP in predefined result
Locator position


}
});



$.post () Way:
$ (' #test_post '). MouseDown (function () {
$.post (
' Login.php ',
{
username:$ (' #username '). Val (),
password:$ (' #password '). Val ()
},
function (data)//postback functions
{
var myjson= ';
Eval (' myjson= ' + data + '; ');
$ (' #result '). HTML ("Name 1:" + myjson.username + "<br/> password 1:" + Myjson.password);
}
);
});



$.get () Way:
$ (' #test_get '). MouseDown (function ()
{
$.get (
' Login.php ',
{
username:$ (' #username '). Val (),
password:$ (' #password '). Val ()
},
function (data)//postback functions
{
var myjson= ';
Eval ("myjson=" + Data + ";");
$ (' #result '). HTML ("Name 2:" + myjson.username + "<br/> Password 2:" + Myjson.password);
}
);
});




}
</script>

<body>
<div id= "Result" style= "background:orange;border:1px solid red;width:300px;height:200px;" ></div>

<form id= "formtest" action= "" method= "POST" >

<p><span> Enter Name: </span><input type= "text" name= "username" id= "username"/></p>
<p><span> input Password: </span><input type= "text" name= "password" id= "password"/></p>
</form>
<button id= "Button_login" >ajax submit </button>

<button id= "Test_post" >post submit </button>

<button id= "Test_get" >get submit </button>



</body>

<?php
echo json_encode (Array (' username ' =>$_request[' username '), ' password ' =>$_request[' password '));
?>

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.