私は次の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");
}
}
Webサービスからの戻り値を読み取れません。コールバック関数(data
パラメーター)の値を出力すると、true
またはfalse
(前の空白に注意してください)を取得します。
印刷するdata.d
と、と表示されますundefined
。参考までに、Webサービス方式は毎回ヒットします。