我在asp.net中有一个简单的web服务,用于确定输入参数是否有效:
[WebMethod]
public bool IsValidNationalCode(string input)
{
return input.IsNationalCode();
}
我通过jquery ajax函数从aspx页面调用它:
$('#txtNationalCode').focusout(function () {
var webMethod = "../PMWebService.asmx/IsValidNationalCode";
var param = $('#txtNationalCode').val();
var parameters = "{input:" + param + "}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if(msg.responseText == true)
$('#status').html("Valid");
else {
$('#status').html("Invalid");
}
},
error: function () {
$('#status').html("error occured");
}
});
});
但我不知道如何获取webservice的返回值以显示适当的消息。这里if(msg.responseText == true)
不起作用
答案 0 :(得分:3)
将IsValidNationalCode方法设为静态,并在javascript中使用:
success: function (msg) {
if (msg.d == true)
$('#status').html("Valid");
else {
$('#status').html("Invalid");
}
}
对于“d”说明,请点击此链接:Never worry about ASP.NET AJAX’s .d again