jQuery实现ajax调用WCF服务的方法(附带demo下载)

2020-05-27 18:07:53易采站长站整理

},
error: function() {
alert('error');
}
});
}
//参数为实体类的方法
function fn2() {
$.ajax({
url: "http://localhost:12079/Service1.svc/GetDataUsingDataContract",
type: "POST",
contentType: "application/json",
data: '{"CityID":1,"CityName":"北京","Seq":"3"}',
dataType: "json",
success: function(returnValue) {
alert(returnValue.CityID + ' ' + returnValue.CityName + "--" + returnValue.Seq);
},
error: function() {
alert('error');
}
});
}
//返回值为类集合的方法
function fn3() {
$.ajax({
url: "http://localhost:12079/Service1.svc/GetList",
type: "POST",
contentType: "application/json",
dataType: "json",
success: function(returnValue) {
for (var i = 0; i < returnValue.length; i++) {
alert(returnValue[i].CityID + ' ' + returnValue[i].CityName+'---'+returnValue[i].Seq);
}
},
error: function() {
alert('error');
}
});
}
function fn4() {
$.ajax({
url: "http://localhost:12079/Service1.svc/GetListData",
type: "POST",
contentType: "application/json",
data: '[{"CityID":1,"CityName":"北京","Seq":"3"},{"CityID":3,"CityName":"上海","Seq":"3"}]',
dataType: "json",
success: function(returnValue) {
for (var i = 0; i < returnValue.length; i++) {
alert(returnValue[i].CityID + ' ' + returnValue[i].CityName + '---' + returnValue[i].Seq);
}
},
error: function() {
alert('error');
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="调用1" onclick="fn1();" /></div>
<input id="Button2" type="button" value="调用2" onclick="fn2();" />
<br />
<input id="Button3" type="button" value="调用3" onclick="fn3();" /></form>
<br />
<input id="Button4" type="button" value="调用4" onclick="fn4();"/>
</body>
</html>

完整实例代码代码点击此处本站下载。

希望本文所述对大家jQuery程序设计有所帮助。