jQuery in Action
Making GET and POST requests
235
Let's look at a simple use of this function as shown in listing 8.5 (which can be
found in the file chapter8/$.get.html).
found in the file chapter8/$.get.html).
<html>
<head>
<title>$.get() Example</title>
<link rel="stylesheet" type="text/css" href="../common.css">
<script type="text/javascript"
src="../scripts/jquery-1.2.1.js"></script>
<script type="text/javascript">
$(function(){
$('#testButton').click(function(){
$.get(
'reflectData.jsp',
{a:1, b:2, c:3},
function(data) { alert(data); }
);
});
});
</script>
</head>
<body>
<button type="button" id="testButton">Click me!</button>
</body>
</html>
Command syntax: $.get
$.get(url,parameters,callback)
Initiates a GET request to the server using the specified URL with any passed parameters as
the query string.
the query string.
Parameters
url
(String) The URL of the server-side resource to contact via the GET method.
parameters
(Object|String) An object whose properties serve as the name/value pairs
used to construct a query string to be appended to the URL, or a preformat-
ted and encoded query string.
used to construct a query string to be appended to the URL, or a preformat-
ted and encoded query string.
callback
(Function) A function invoked when the request completes. The response
body is passed as the first parameter to this callback, and the status as
the second.
body is passed as the first parameter to this callback, and the status as
the second.
Returns
The XHR instance.
The XHR instance.
Listing 8.5
Using $.get() utility function to fetch data from the server
Gets data from
the server
the server
b