jQuery - AJAX get() ? post() ???
HTTP ??: GET ? POST
????? ? ?? ? ??-??? ? ?? ???? ??? GET ? POST???.
GET - ??? ????? ??? ?? POST - ??? ???? ??? ???? ??
GET? ????? ???? ???? ????(??) ? ?????. ??: GET ???? ??? ???? ??? ? ????.
POST? ???? ???? ???? ??? ?? ????. ??? POST ???? ???? ???? ??? ??? ?? ???? ??? ? ?? ?????.
Get? Post? ???
Get ???:
??? ???? get ???? ???? ??? ? ??? ????? ??? 1KB? ?????. ???? URL? ???? ?????. (HTTP ?? ??) ?, ????? ? ?? ?? ??? ?? ???? URL ???? ???? ?? ??? ??? ??? ?????. ?? ??? ?? ?????? ????? ???? ?? ???? ???? ???? ?? ??, ???? ?? ?? ??? ???? ?? ? ??? ????. ??? ?? ???? get ???? ??? ?? ??? ??? ? ????.
Post ???:
POST ???? ??? ? ????? ? ?? ?? ??? ?? ???? URL ??? ?? ??? ???? ?? HTTP ???? ??? ???? ? ??? ????. . POST ?? GET ??? ???? ???? ???? ?? GET ??? ???? ???? ??? ??? ?? ???.
??? ??? GET ??? ?? ?? ???? ???? ?? ???? ??? ??? ?? ????? POST? ?? ? ?????.
$.get() ???
$.get() ???? HTTP GET ??? ?? ????? ???? ?????.
??: ??
$.get(URL,callback);
?? URL ????? ????? URL? ?????.
???? ?? ????? ??? ??? ? ??? ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.get("/try/ajax/demo_test.php",function(data,status){ //需要引入demo_test.php文件 alert("數(shù)據(jù): " + data + "\n狀態(tài): " + status); }); }); }); </script> </head> <body> <button>發(fā)送一個GET 請求并獲取返回結(jié)果</button> </body> </html>
$.get()? ? ?? ????? ????? URL("demo_test.php")???.
? ?? ????? ?? ?????. ? ?? ?? ????? ??? ???? ??? ????, ? ?? ?? ????? ?? ??? ?????.
? PHP ??("demo_test.php")? ??? ????:
<?php echo "這是個從PHP文件中讀取的數(shù)據(jù)"; ?>
$.post() ???
$.post() ???? HTTP POST? ?? ???? ???? ?????. ?? .
??:
$.post(URL,data,callback);
?? URL ????? ????? URL? ?????.
???? ??? ????? ??? ?? ??? ???? ?????.
???? ?? ????? ??? ??? ? ??? ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.post("/try/ajax/demo_test_post.php",{ name:"php中文網(wǎng)", url:"http://ipnx.cn" }, function(data,status){ alert("數(shù)據(jù): \n" + data + "\n狀態(tài): " + status); }); }); }); </script> </head> <body> <button>發(fā)送一個 HTTP POST 請求頁面并獲取返回內(nèi)容</button> </body> </html>
$.post()? ? ?? ????? ????? URL("demo_test_post.php")???.
?? ?? ??(?? ? ??)? ?? ???? ????.
"demo_test_post.php"? PHP ????? ??? ????? ?? ??? ? ??? ?????.
? ?? ????? ?? ?????. ? ?? ?? ????? ??? ???? ??? ????, ? ?? ????? ?? ??? ?????.
? PHP ??("demo_test_post.php")? ??? ????:
<?php $name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; $city = isset($_POST['url']) ? htmlspecialchars($_POST['url']) : ''; echo '網(wǎng)站名: ' . $name; echo "\n"; echo 'URL 地址: ' .$city; ?>
??? $.post() ?:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> function checkname(){ if($('#name').val() == ""){ $('#msg').html("please enter the name!"); $('#name').focus; return false; } if($('#address').val() == ""){ $('#msg').html("please enter the address!"); $('#address').focus; return false; } ajax_post(); } function ajax_post(){ $.post("text.php",{name:$('#name').val(),address:$('#address').val()}, function(data){ //$('#msg').html("please enter the name!"); //alert(data); $('#msg').html(data); }, "text"); } </script> </head> <body> <form id="ajaxform" name="ajaxform" method="post" action="text.php"> <p> name<input type="text" name="name" id="name"/> </p> <p> address<input type="text" name="address" id="address"/> </p> <p id="msg"></p> <p> <input name="Submit" type="button" value="submit" onclick="return checkname()"/> </p> </form> </body> </html>
text.php ?? ???:
<?php $name = $_POST["name"]; $address = $_POST["address"]; echo $name."<br>"; echo $address."<br>"; echo "success"; ?>