</pre><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>1、$.get()</strong></span><p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="background-color: rgb(184, 209, 0);">$.get( url, [ data], [callback] )</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">url:string类型,ajax请求的地址。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">data:可选参数,object类型,发送至服务器的key/value数据会作为QueryString附加到请求URL中。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">callback:可选参数,function类型,当ajax返回成功时自动调用该函数</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><pre name="code" class="javascript">$.get("submit.php",{id: '123',name:'xiaolu'},
function(data,state){
alert(data);
alert(state);
}
)
$.post(url, [data], [callback], [type])
type:type为请求的数据类型,可以是html,xml,json等类型,如果我们设置这个参数为:json,那么返回的格式则是json格式的,如果没有设置,就和$.get()返回的格式一样,都是字符串的。
$.get("submit.php",{id: '123',name:'xiaolu'},
function(data,state){
alert(data);
alert(state);
},
"json")
3、$.getJSON()
支持跨域调用
getJSON(url,[data],[callback])
JSON是一种理想的数据传输格式,它能够很好的融合与JavaScript或其他宿主语言,并且可以被JS直接使用。使用JSON相比传统的通过 GET、POST直接发送“裸体”数据,在结构上更为合理,也更为安全。至于jQuery的getJSON()函数,只是设置了JSON参数的 ajax()函数的一个简化版本。这个函数也是可以跨域使用的,相比get()、post()有一定优势。另外这个函数可以通过把请求url写 成"myurl?callback=X"这种格式,让程序执行回调函数X。
4、$.ajax()
$.ajax()是jquery中通用的一个ajax封装
$.ajax(options)
其中options是一个object类型
$.ajax({
url: 'submit.php',
datatype: "json",
type: 'post',
success: function (e) { //成功后回调
alert(e);
},
error: function(e){ //失败后回调
alert(e);
},
beforeSend: function(){ /发送请求前调用,可以放一些"正在加载"之类额话
alert("正在加载");
}
})