Java培训之javaweb课程jquery中的ajax

jquery使得js变得更加简单易用。那么在jquery中是如何发送ajax请求的呢?

主要讲解jquery以下的几个方法。

$.get()、$.post()、$.getJSON()、$.ajax();

1 $.get();

jquery中发送get请求的方法

方法签名:

$.get(url, [data], [callback], [type])

[]表示参数可选

参数解析:

url:发送的请求地址

data:待发送 Key/value 参数。

callback:请求成功时回调函数。

type:返回内容格式,xml, html, script, json, text, _default。

发送示例:

$.get(“user?method=login”,{username:”lll”,password:”lll”},function(data){

                          alert(data.errCode);

                  },”json”);

2 $.post();

jquery中发送post请求的方法。

方法签名:

$.post(url, [data], [callback], [type])

[]表示可选参数

参数解析:

url:发送请求地址。

data:待发送 Key/value 参数。

callback:发送成功时回调函数。

type:返回内容格式,xml, html, script, json, text, _default。

发送示例:

$.post(“user?method=login”,{username:”lll”,password:”lll”},function(data){

                          alert(data.errCode);

},”json”);

3 $.getJSON();

jquery中返回json数据的get请求

方法签名:

$.getJSON(url, [data], [callback])

[]表示可选参数

参数解析:

url:发送请求地址。

data:待发送 Key/value 参数。

callback:载入成功时回调函数。

发送示例:

$.getJSON(“user?method=login”,{username:”lll”,password:”lll”},function(data){

                          alert(data.errCode);

         })

4 $.ajax()

jquery中底层的ajax请求方法,可以设置详细的参数

方法签名:

$.ajax(url,[settings])

[]表示可选参数

参数解析:

url:发送请求地址。

settings:其他详细设置。可设置项参加jquery文档

发送示例:

$.ajax({

   type: “POST”,

   url: “some.php”,

   data: “name=John&location=Boston”,

   success: function(msg){

     alert( “Data Saved: ” + msg );

   };

         error:function(){

 alert(“请求失败”)

}

});

想要了解跟多关于Java培训课程内容欢迎关注尚硅谷Java培训,尚硅谷除了这些技术文章外还有免费的高质量Java培训课程视频供广大学员下载。


上一篇:
下一篇: