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 });
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With