Home > Article > Web Front-end > Example analysis of get() and post() in ajax application in jquery
get and post send data in different ways, but in jquery's ajax this difference is not visible to the user. There is a limit to the size of data transmitted by get. The data requested by get will be cached by the browser. The two methods are received differently on the server side.
get()
The get function form in jquery is $.get(URL,data,callback,type);
The code is as follows:
$.get("web.jsp",{ name:"name1", //get方法中的数据不仅可以是映射方法,也可以是"name=name1&age=12"的字符串方式, age:"12" //如果有中文,要使用编码,"name="+encodeURIComponent("栾鹏")+"&age=12"}, function(data,textStatue){ //data表示返回的内容,可以是xml,JSON文件,HTML片段。textStatus表示请求状态:sucess,error,notmodified,timeout4种 $("#p1").html(data); //如果是html片段,直接设置代码段 username = $(data).find("comment").attr("username"); //如果是xml文档,则使用$转化为dom对象 username = data.username; //如果是json数据,当成对象使用});
post()
The post function form in jquery is $.post(URL,data,callback);
The code is as follows:
$.post("web.jsp", $("#form1").serialize(), //serialize序列化表单内容,作为jQuery的表单对象的函数。 function(data,textStatue){ //data表示返回的内容,可以是xml,JSON文件,HTML片段。textStatus表示请求状态:sucess,error,notmodified,timeout4种 $("#p1").html(data); //如果是html片段,直接设置代码段 username = $(data).find("comment").attr("username"); //如果是xml文档,则使用$转化为dom对象 username = data.username; //如果是json数据,当成对象使用});
The above is the detailed content of Example analysis of get() and post() in ajax application in jquery. For more information, please follow other related articles on the PHP Chinese website!