TOC

This article is currently in the process of being translated into Vietnamese (~17% done).

AJAX:

The get() and post() methods

Phương thức get() và post() của jQuery cho phép bạn dễ dàng gửi HTTP request tới trang và có kết quả trả về. Khi bạn post một form thì thường là GET hoặc POST, tương ứng vơi phương thức get() và post() trong jQuery.

The two methods are pretty much identical, since they simply just invoke different request types against the server. They are both static methods, which means that instead of instantiating a jQuery object and then working with that, we call get() or post() directly on the jQuery class, either by writing jQuery.get() or by using the shortcut character like this: $.get(). In its most simple form, the get() and post() methods takes a single parameter, which is the URL that you wish to request. However, in most cases you will want to do something with the returned data, in which case you can pass a callback function as a parameter, which jQuery will call if the request succeeds.

Let's do some testing. In the previous chapter, I created an HTML file called "content.html", which we loaded using the jQuery load() method. When testing the following example, make sure that you have a file called "content.html" in the same directory as the file in which you have the example. The content doesn't really matter, just write anything in there really. Here's an example of the get() method:

<script type="text/javascript">
$(function()
{
	$.get("content.html", function(data, textStatus)
	{
		alert("Done, with the following status: " + textStatus + ". Here is the response: " + data);
	});
});
</script>

The first parameter is the URL, which is just content.html. The second parameter is more interesting. It's a callback function, which jQuery calls if the page request succeeds. The first callback parameter is simply the content of the page requested, while the second callback parameter is the textual status of the request.

Bạn có thể request một trang HTML đơn giản nhưng thông thường bạn dùng GET hay POST để truyền vào một số tham số sau đó được xử lý bởi server, có thể là code PHP, ASP hay ASP.NET.

<script type="text/javascript">
$(function()
{
	$.post("test_post.php",
	{
		name: "John Doe",
		age: "42"
	},
	function(data, textStatus)
	{
		alert("Response from server: " + data);
	});
});
</script>

This example is much like the first one, but we make the POST request to another page, in this example a PHP page, and as the second parameter, we pass in a map of POST parameters. The map is constructed of two parameters, a name and an age. If we had used a GET request instead of a POST request (POST requests doesn't take parameters from the URL like GET does), the above code would actually have corresponded to requesting an URL like this in your browser:

test_get.php?name=John Doe&age=42

The PHP script can then read the parameters, process them and return a result. The script on our server simply takes the two values and creates a string like "<name> is <age> years old" and then returns it, which you can see if you test the example above.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!