62,266
社区成员
发帖
与我相关
我的任务
分享
[ScriptService]
[WebService(Namespace = "http://www.midaske.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SyncService : System.Web.Services.WebService
{
public SyncService()
{
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Hello()
{
return "Hello Test";
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript" language="javascript">
$.ajax({
url: "Services/SyncService.asmx/Hello",
type: "post",
dataType: ($.browser.msie) ? "text" : "xml",
success: function (data) {
alert("success");
var xml;
if (typeof data == "string") {
alert("1");
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
alert("2");
xml = data;
}
var ser = new XMLSerializer();
alert(ser.serializeToString(xml));
},
error: function (e) {
alert("error");
alert(e.toString());
}
});
</script>
</body>
</html>
<script type="text/javascript">
$(function () {
/*
1、WebService请求类型都为Post,WebService的Url为“[WebServiceUrl]/[WebMethod]”
2、contentType声明为Json
3、data要用Json的字符串格式传入
4、设置了dataType为json后,result就直接为返回的Json对象。
*/
//调用无参数方法
$("#btnHelloWorld").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/HelloWorld",
data: "{}",
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
});
//传入1个参数
$("#btnHello").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/Hello",
data: "{name:'KiMoGiGi'}",
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
});
//返回泛型列表
$("#btnArray").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/CreateArray",
data: "{i:10}",
dataType: 'json',
success: function (result) {
alert(result.d.join(" | "));
}
});
});
//返回复杂类型
$("#btnPerson").click(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetPerson",
data: "{name:'KiMoGiGi',age:26}",
dataType: 'json',
success: function (result) {
var person = result.d;
var showText = [];
for (var p in person) {
showText.push(p + ":" + person[p]);
}
alert(showText.join("\r\n"));
}
});
});
});
</script>
$.ajax({
url: "WebService1.asmx/WS3",
dataType: "json",
data: "",
beforeSend: function(x) {
x.setRequestHeader("Content-Type", "application/json; charset=utf-8");
},
success: function(json) {
alert(json.d);
},
error: function(x, e) {
alert(x.responseText);
},
complete: function(x) {
alert(x.responseText);
}
});