Reputation: 427
Please let me know if below is the correct way to send data to another page using jQuery or not. I am a beginner and not sure about the syntax. rowData
is in JSON format. I am passing the values on click of a button.
$.post("GridValues.jsp", {"rowData": rowData });
Upvotes: 5
Views: 2407
Reputation: 340055
The syntax for your POST
depends on whether the JSP page is expecting to receive CGI-style URI-encoded variables, or a JSON blob.
In the former (more common) case, your code is correct - the rowData
variable will appear as a CGI parameter which will need to be JSON decoded.
If you wish to have your client code do something after the $.post
call has completed then the modern way of doing that is to use deferred objects:
$.post(...).done(function(data, textStatus, jqXHR) {
// called on success
}).fail(function(jqXHR, textStatus, errorThrown) {
// called on failure
}).always(function() {
// called in both cases
});
Note how using deferred objects exposes features (.fail
and .always
callbacks) that are not directly available using $.post
, which only supports passing a success
(aka done
) handler.
Using the jqXHR
result of $.post
to register callbacks is more flexible than passing callbacks as parameters directly to the AJAX-related methods. It allows you to write functions that are only responsible for initiating the AJAX call and then have handlers that are completely decoupled from that responsible for processing the results.
Upvotes: 6
Reputation: 45134
Your jquery call should work fine for you. Plus you can always refer the jquery API jQuery.post
jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )
urlA string containing the URL to which the request is sent.
dataA map or string that is sent to the server with the request.
success(data, textStatus, jqXHR)A callback function that is executed if the request succeeds.
dataTypeThe type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
Try them out and let us know if you come across any problems.
Upvotes: 3
Reputation: 27317
This syntax is valid.
See: http://api.jquery.com/jQuery.post/
the doc supplies this syntax:
jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )
Are you sure you don't want to include a success handler?
Upvotes: 1
Reputation: 8476
$.post("GridValues.jsp", {"rowData": rowData }, function(){} );
it is same as
$.ajax({
type: 'POST',
url: "GridValues.jsp",
data: {"rowData": rowData },
success: function(){}
});
Upvotes: 2