ตารางเปรียบเทียบ $.get() กับ $.post()
$.get() | $.post() |
รับข้อมูลจากไฟล์ที่ระบุ | ส่งข้อมูลไปประมวลผลไฟล์ที่ระบุ |
ไวยากรณ์
- $.get(URL, callback);
- $.post(URL, data, callback);
ไฟล์ demo.asp
<% response.write("This is some text from an external ASP file.") %>
1.$.get()
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.get("demo.asp", function(data, status){ alert("Data: " + data + "\nStatus: " + status); }); }); }); </script> </head> <body> <button>Send an HTTP GET request to a page and get the result back</button> </body> </html>
ไฟล์ที่ 1 test_get.php
ไฟล์ demo2.asp
<% dim fname,city fname=Request.Form("name") city=Request.Form("city") Response.Write("Dear " & fname & ". ") Response.Write("Hope you live well in " & city & ".") %>
2.$.post()
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $.post("demo2.asp", { name: "Donald Duck", city: "Duckburg" }, function(data,status){ alert("Data: " + data + "\nStatus: " + status); }); }); }); </script> </head> <body> <button>Send an HTTP POST request to a page and get the result back</button> </body> </html>
ไฟล์ที่ 2 test_post.php