I have the following jQuery code:
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);
}
And a simple Web Service:
[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");
}
}
I just can't read the return value from my web service. When I print out the value in the callback function (data
parameter) I get true
or false
(note the preceding whitespace).
When I print out data.d
, it says undefined
. FYI, the web service method gets hit each time.