我有以下 jQuery 代码:
var isUsernameAvailable = false;
function CheckUsername(uname) {
$.ajax({
method: "POST",
url: "IsUsernameAvailable.asmx/IsAvailable",
data: { username: uname },
dataType: "text",
success: OnSuccess,
error: OnError
}); // end ajax
} // end CheckUsername
function OnSuccess(data) {
if (data == true) { // WHY CAN'T I TEST THE VALUE
isUsernameAvailable = true;
} else {
isUsernameAvailable = false;
$('#error').append('Username not available');
}
}
function OnError(data) {
$('#error').text(data.status + " -- " + data.statusText);
}
还有一个简单的 Web 服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class IsUsernameAvailable : System.Web.Services.WebService {
public IsUsernameAvailable () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public bool IsAvailable(string username) {
return (username == "xxx");
}
}
我只是无法从我的网络服务中读取返回值。当我打印出回调函数(data
参数)中的值时,我得到true
或false
(注意前面的空格)。
当我打印出来时data.d
,它说undefined
。仅供参考,网络服务方法每次都会受到打击。