问题
I have some jQuery that sends data to a AddComment.php page. The data is a user id (could be gotten through a session), an item id and a comment under text form.
In this case, should all data be sent through a POST? Are there cases where one might find a mix of GET and POST?
回答1:
Go read RFC 2616.
There are very specific semantics associated with GET and POST which have a big impact on caching and on logging.
In the case of your example, the data to be added should be sent in a POST. Whether a reference to the item being commented on should be sent via POST or GET variable is debatable. (you can POST to a URL with a query string and any competent web language should be able to discriminate between the same variable name sent via both methods)
A more transparent example of when GET and POST should be mixed is when POSTing to a front-controller - here the same path is used by various different bits of functionality (web pages if you like). The specific bit of functionality being invoked is indicated by the query in a GET operation. If the selection criteria are moved into the POST then you have to cater for both scenarios within the front controller, and you lose resolution of the functionality in the log files.
回答2:
It should be sent by POST primarily because a comment can exceed the length of a URL a web browser will access. It should also be sent by POST because this form creates or modifies data, and that's the convention. Search spiders will not make POST requests, for example, to avoid doing that.
You cannot mix GET and POST. An HTTP request is of only one method.
回答3:
Actually i think that if you do:
var id= $('#id').val();
var itemid= $('#itemid').val();
var comment= $('#comment').val();
var url = 'AddComment.php?id='+id;
var data = { itemid: itemid, comment: comment }
$.post(url, data, successFunction);
your itemId will be sent throu POST and the ID through GET and php will recognize it (i sometimes do that using the action of the form but not through ajax).
BTW - why do you need this?Why not send everything through POST?
来源:https://stackoverflow.com/questions/6730186/should-all-data-be-sent-by-http-post