jQuery 笔记 —— AJAX:ajax()、get()、post()
一、$.get() 方法
jQuery.get(url,[data],[callback],[type])
url:请求的页面地址
data:待发送的Key/value参数
callback:成功时的回调函数
type:返回内容的格式,html、xml、script、json、text、_default
示例:
$.get(
"test.html",
{key1:"value1",key2:"value2"},
function(data){alert("加载的数据"+data)},
"html");
二、$.post() 方法
jQuery.post(url,[data],[callback],[type])
url:请求的页面地址
data:待发送的Key/value参数
callback:成功时的回调函数
type:返回内容的格式,html、xml、script、json、text、_default
示例:
$.post(
"test.html",
{key1:"value1",key2:"value2"},
function(data){alert("加载的数据"+data)},
type:"html");
注:jQuery1.12中方法支持对象参数
三、$.ajax() 方法
jQuery.ajax(url,[setting])
ajax方法参数为JSON数据
url:请求的页面地址
setting:所有设置选项,常用:type、url、data、dataType、cache、beforeSend事件句柄、error句柄、dataFilter句柄、success句柄、complete句柄。
示例:
$.ajax({
url:"test.html",
type:"GET",
data:{key1:"value1",key2:"value2"},
cache:false,
success:function(data){alert("加载的数据"+data)},
dataType:"html"
});
四、注意项
1、$.ajax() 参数为json对象,而$.get() 与$.post() 直接为参数列表
2、url 属性只能是相对位置,且不能带工程路径,也不能以”/“ 开始,如SpringMVC 的某个Controller的方法访问路径为:
localhost:8080/MyProject/request/testMethod,其中dispatcherServlet 映射路径为”/*“,Controller映射路径为”/request”,该Controller中方法映射路径为”/testMethod”,则ajax、get、post方法中url 应为:request/testMethod,不能为:/request/testMethod 或带上工程目录。
具体见:http://jquery.cuishifeng.cn/jQuery.Ajax.html
还没有评论,来说两句吧...