异步请求返回的内容格式:文本,xml,json。javascript
能够返回简单的对象html
异步调用:java
使用webservice的异步调用:jquery
(1)返回文本web
<html>ajax
<head>json
<title></title>app
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>异步
<script type="text/javascript">ide
$(function (){
$("#Button1").click(function(){
$.ajax({
contentType:"application/json",
type:"POST",
url:"webService.asmx/Select",---调用webservice里面的select方法
data:"{name:'"+$("#Text1").val()+"'}",
dataType:"json",
success:function(result){
alert(result.d);
}//经过webservice请求数据,返回文本
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Text1" type="text" /><input id="Button1" type="button" value="button" />
</div>
</form>
</body>
</html>
(2)返回一个集合
<html>
<head>
<title></title>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function (){
$("#Button1").click(function (){
$.ajax({
contentType:"application/json",
type:"POST",
url:"WebService.asmx/SelectList",//是方法名称
data:"{name:'"+$("#Text1").val()+"'}",
dataType:"json",
success:function(result){
alert(result.d.join()); join中间不须要添加","号
}//经过webservice请求数据返回集合
下面是webservice的内容:
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要容许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
static List<string> list=new List<string>();
public WebService () {
//若是使用设计的组件,请取消注释如下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string Select(string name)
{
return name+"已经存在";
}
[WebMethod]
public List<string> SelectList(string name)
{
list.Add(name);
return list;
}
}
});
});
});
</script>
</head>
<body>
<div>
<input id="Text1" type="text" /><input id="Button1" type="button" value="button" />
</div>
</body>
</html>
直接ajax请求数据:
<html>
</html>