XmlHttpRequest – Choose GET or POST?
Dflying | 10 March, 2006 20:24XmlHttpRequest supports both GET and POST requests to the server, which one should we use? As usual, GET is light and POST is heavy. But I'd say, it really depends on your scenario.
GET requests pass parameters to the server using the query string. So we need to be careful about:
- length: total query string length. GET Url strings cannot be longer than 2048 chars.
- i18n: Special international characters passed from the browser to the server using XmlHttpRequest need to be Url encoded (since they are passed using the GET Url string). JavaScript provides the built-in encodeURIComponent() function for this.
- caching: XmlHttpRequest GET requests are cached if browser caching is turned on (the default setting for both IE and FireFox). So, in some cases, after the first XmlHttpRequest to the specified Url, all subsequent results are cached and the browser never even tries to reach the server. While this might be what you want in some cases, in others you do need to poll the data every time. There are two workarounds to this problem - you can append a timestamp to your Url to make it unique or use the following server-side code in your server page:
Server Side Code (C#)Response.Cache.SetCacheability( System.Web.HttpCacheability.NoCache);
Client Side Code(JavaScript)
var callBackUrl = "AjaxHandler.aspx?var=value"; callBackUrl = callBackUrl + "&timeStamp=" + encodeURIComponent((new Date()).getTime()); xmlRequest.open("GET", url, true);
POST requests pass parameters to the server using forms. You do not have the GET 2048 query string limitation and can easily pass all form data from the current form to the server. You can even pass the ASP.NET ViewState of the page to the server and try playing with control state and the ASP.NET lifecycle, which is really cool. However, passing all form data will slow down the response time, so you do need to be careful what you pass to the server.
Posted in
JavaScript & AJAX.
Comment: (4).
Trackbacks:(100).
Permalink
«Next post |
Previous post»
Referers
Comments
-
1. blueoxygen | 03/11,2006 at 00:38
IE's cache whe using XHR object and GET mothod is really self-assertive.FF will leave the decision to the developer.
-
2. fcuznxer | 10/17,2008 at 01:30
ucrjseyr http://egmdalcp.com knqlbnxn ksenzbec jzhejshs [URL=http://ieeaskow.com]ukygfgqj[/URL]
-
3. tkexvmxw | 10/25,2008 at 02:37
bxqynusa [URL=http://iboyczji.com]pjbncflk[/URL] reotyyuy http://iipodcqo.com aocgjbpz yvwxgjlw
-
4. eafogszt | 10/28,2008 at 09:29
xkddqqgf http://thmdtbyd.com cykynaqh hysmhztu [URL=http://lcbimcmc.com]rmhngxil[/URL] rmfncxcv




