admin管理员组

文章数量:1278859

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 });

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 });
Share Improve this question edited Oct 15, 2012 at 9:16 user2428118 8,1144 gold badges46 silver badges73 bronze badges asked Oct 15, 2012 at 9:11 user1710288user1710288 4272 gold badges9 silver badges15 bronze badges 4
  • stackoverflow./questions/how-to-ask – Jacob George Commented Oct 15, 2012 at 9:12
  • 2 docs.jquery./Ajax/jQuery.post – John Gathogo Commented Oct 15, 2012 at 9:13
  • 1 The question should probably rather be: "How do I interpret the API documentation". – phant0m Commented Oct 15, 2012 at 9:20
  • api.jquery./jquery.post – NoWar Commented Jul 28, 2015 at 1:25
Add a ment  | 

4 Answers 4

Reset to default 6

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 mon) 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 pleted 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 pletely 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 e across any problems.

$.post("GridValues.jsp", {"rowData": rowData }, function(){} );  

it is same as

$.ajax({
type: 'POST',
url: "GridValues.jsp",
data: {"rowData": rowData },
success: function(){}
});

This syntax is valid.

See: http://api.jquery./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?

本文标签: javascriptpost syntax in jQueryStack Overflow