4 common request methods of ajax in jQuery: 1 .$.ajax() returns the XMLHttpRequest object it created. $.ajax() has only one parameter: the parameter key / value object, which contains the configuration and callback function information. If the dataType option is specified, make sure that the server returns the correct MIME information . example
$.ajax({ type: "post", dataType: "html", url: '/Resources/GetList.ashx', data: dataurl, success: function (data) { if (data != "") { $("#pager").pager({ pagenumber: pagenumber, pagecount: data.split("$$")[1], buttonClickCallback: PageClick }); $("#anhtml").html(data.split("$$")[0]); } } });
2. Load information via remote HTTP GET request. This is a simple GET request function to replace the complex $.ajax. The callback function can be called when the request is successful. But execute the function on error, use $.ajax. example:
$.get("test.cgi", { name: "liu'jun", time: "2pm" }, function(data){ alert("Data Loaded: " + data); });
3. Load information via remote HTTP POST request. This is a simple POST request function to replace the complex $.ajax. The callback function can be called when the request is successful. If you need to execute a function on error, use $.ajax. example
$.post("/Resources/addfriend.ashx", { "fid": fids, "fname": fnames, "tuid": tuids, "tuname": tunames }, liu jun function (data) { if (data == "ok") { alert("Added successfully!"); } })
4. Load JSON data via HTTP GET request. example
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){ $.each(data.items, function(i,item){ $("<img/>").attr("src", item.media.m).appendTo("#images"); if ( i == 3 ) return false; }); });