<script type="text/javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
<script type="text/javascript">
$(function () {
$("#btn").click(function () {
$.ajax({
type: "POST",
url: "data.asmx/GetUser",
data: { orderType: "asc" }, //注意这个地方的orderType是调用da.asmx(webservice)中GetUser的参数
success: function (data) {
debugger
//将返回字符串转换为json对象
//如果
var json = null;
try {
json = eval('(' + data.text + ')');
} catch (e) {
alert("返回字符串不是json格式!");
return;
}
alert(json.d);
}
});
})
})
</script>
<div>
<h1>
Jquery+WebService=》返回Json类型</h1>
<hr />
<input id="txt" type="text" size="20" runat="server" />
<input id="btn" type="button" value="调用WebServer方式111" />
<hr />
</div>
<script type="text/javascript">
$(function () {
$("#btn").click(function () {
$.ajax({
type: "POST",
url: "data.asmx/GetUser",
data: { orderType: "asc" }, //注意这个地方的orderType是调用da.asmx(webservice)中GetUser的参数
success: function (data) {
debugger
//将返回字符串转换为json对象
//如果
var json = null;
try {
json = eval('(' + data.text + ')');
} catch (e) {
alert("返回字符串不是json格式!");
return;
}
alert(json.d);
}
});
})
})
</script>
<div>
<h1>
Jquery+WebService=》返回Json类型</h1>
<hr />
<input id="txt" type="text" size="20" runat="server" />
<input id="btn" type="button" value="调用WebServer方式111" />
<hr />
</div>
Webservice代码(data.asmx)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;//添加它为了方便序列化
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService()]//如果不添加它,Json访问WebService会出错
public class data : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetUser(string orderType)
{
User use1 = new User
{
ID = 1,
Name = "GXW",
PWD = "123456"
};
User use2 = new User
{
ID = 2,
Name = "GXW2",
PWD = "23456"
};
User use3 = new User
{
ID = 3,
Name = "3GXW2",
PWD = "3456"
};
List<User> list = new List<User>();
list.Add(use1);
list.Add(use2);
list.Add(use3);
return ToJSON(list);
}
//对数据序列化,返回JSON格式
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;//添加它为了方便序列化
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService()]//如果不添加它,Json访问WebService会出错
public class data : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetUser(string orderType)
{
User use1 = new User
{
ID = 1,
Name = "GXW",
PWD = "123456"
};
User use2 = new User
{
ID = 2,
Name = "GXW2",
PWD = "23456"
};
User use3 = new User
{
ID = 3,
Name = "3GXW2",
PWD = "3456"
};
List<User> list = new List<User>();
list.Add(use1);
list.Add(use2);
list.Add(use3);
return ToJSON(list);
}
//对数据序列化,返回JSON格式
public string ToJSON(object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
}
public class User
{
public int ID { get; set; }
public string Name{get;set;}
public string PWD { get; set; }
}
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
}
public class User
{
public int ID { get; set; }
public string Name{get;set;}
public string PWD { get; set; }
}