常用函数:$.get()、$.post()、$.ajax()
各交互方法介绍: 1、get方式 语法:$.get(url, [data], [callback], [type])
1)url:待载入页面的URL地址 2)data:待发送 Key/value 参数。 3)callback:载入成功时回调函数。 4)type:返回内容格式,xml, html, script, json, text, _default。2、post方式 语法:$.post(url, [data], [callback], [type]) 1)url:发送请求地址
2)data:待发送 Key/value 参数。 3)callback:载入成功时回调函数。 4)type:返回内容格式,xml, html, script, json, text, _default。 $.post(url, jsondata, function(data){ if(data.success){ xxx }else{ xxx })3、ajax方式 语法:$.ajax(url,[settings]) 1)回调函数
如果要处理$.ajax()得到的数据,则需要使用回调函数。beforeSend、error、dataFilter、success、complete。 beforeSend 在发送请求之前调用,并且传入一个XMLHttpRequest作为参数。 error 在请求出错时调用。传入XMLHttpRequest对象,描述错误类型的字符串以及一个异常对象(如果有的话) dataFilter 在请求成功之后调用。传入返回的数据以及"dataType"参数的值。并且必须返回新的数据(可能是处理过的)传递给success回调函数。 success 当请求之后调用。传入返回后的数据,以及包含成功代码的字符串。 complete 当请求完成之后调用这个函数,无论成功或失败。传入XMLHttpRequest对象,以及一个包含成功或错误代码的字符串。需要引入文件 ajaxfileupload.js
$.ajax({ type:"POST", url:"some.php", data:"name=John&location=Boston", success:fucntion(msg){ alert("Data Saved" + msg); } });附加函数: $.trim(str) – 除去变量两端空格 1)content.trim() – 除去content首尾空格字符 – IE下会报错 2)$.trim(str) – 将str首尾空格字符除去,返回字符串