Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.post syntax in jQuery

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 });
like image 817
user1710288 Avatar asked Dec 15 '22 18:12

user1710288


2 Answers

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.

like image 87
Alnitak Avatar answered Dec 18 '22 10:12

Alnitak


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.

like image 33
Techie Avatar answered Dec 18 '22 09:12

Techie