0

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.

Robotronx
  • 1,728
  • 2
  • 21
  • 43

1 Answers1

1

You have the dataType specified as text. This would lead me to believe that the response you're getting is actually a string and not a boolean value. Switching the dataType to json would give you a Javascript object.

Edit: The reason for the parsererror is because the boolean that you are returning is actually a .NET boolean converted to a string. When .ToString() ends up being called for the actual HTTP response, it ends up being True or False. In JSON, boolean values are true or false (notice the casing). When jQuery is trying to parse the response, it doesn't think it's a correct boolean and throws the error.

Depending on what ASP.NET flavor you are using, you have a few options. If you're using MVC or the Web API, just return like this:

return Json(username == "xxx", JsonRequestBehavior.AllowGet);

If you aren't using either of those technologies, then it might be best to change your return type to a string and then call:

return new JavaScriptSerializer().Serialize(username == "xxx");

You will need to import the System.Web.Script.Serialization namespace for the previous code snippet to work.

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
  • I did what you suggested with `return new JavaScriptSerializer().Serialize(username == "xxx");` and `dataType: "text"` and now in OnSuccess() I test for 'true' (string value) but nothing... The OnSuccess() does get hit, but none of the conditions. – Robotronx Oct 12 '12 at 21:27
  • @robotron - The `dataType` should not be text; it should be json. – Justin Helgerson Oct 12 '12 at 21:28
  • @robotron - What is the **exact** data you are getting back from the server? – Justin Helgerson Oct 12 '12 at 21:36
  • When I debug the WebService it shows it's returning either "true" or "false". What gets to the browser, I don't know. – Robotronx Oct 12 '12 at 21:46
  • @robotron - You should find out. If using Firefox, download FireBug and you will be able to see. – Justin Helgerson Oct 12 '12 at 21:50
  • I've dug up this (with Chrome): ` false` How can it be that it's sending back XML? I did everything as stated above. – Robotronx Oct 12 '12 at 22:01
  • @robotron - See http://stackoverflow.com/questions/211348/how-to-let-an-asmx-file-output-json. If you can upgrade to MVC/Web API your life will be much more enjoyable. – Justin Helgerson Oct 12 '12 at 22:11
  • It's easy to write out the value received from the web service, but to test against it... Impossible. Maybe I should do some basic research, I have a feeling I've got the whole thing wrong. Thank you, nevertheless. – Robotronx Oct 12 '12 at 22:25