JQuery 调用 .net WebService
[WebService(Namespace = "http://tempuri.org/")]
//JQuery 调用
(function JQCallWS(){
$.ajax({
url: "http://localhost:8817/lqbz/WebService_test.asmx/ImportStudent",
type: "post",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: "{'students':[{'name':'KoBe','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}",
success: function(result) {
alert("JQ" + result);
},
error: function(e) {
alert(e.responseText);
}
});
})();
//js 调用
function ajaxFunction() {
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
alert("您的浏览器不支持AJAX!");
xmlHttp = false;
}
}
}
return xmlHttp;
}
(function(){
var xmlHttp = ajaxFunction();
xmlHttp.open("POST","http://localhost:8817/lqbz/WebService_test.asmx/ImportStudent",true);
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=utf-8");
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){
num = xmlHttp.readyState;
response = xmlHttp.responseText;
alert(response);
}
}
xmlHttp.send("{'students':[{'name':'KoBe','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}");
})();
需要注意的是,服务端参数名需要和客户端Json数组的key值相同