views:

270

answers:

2

Hey,

I have this AJAX code, but it doesn't seem to throw the 'alert' method. Instead, nothing happens. I looked at it with Fiddler and got this error message: {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}

I'm trying to call a web method in the code-behind called MyWebMethod:

 $.ajax({   type: "POST",
            url: "Test.aspx/MyWebMethod",
            data: "{" + username + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function() {
                alert("success");
            },

            fail: function() {
                alert("Fail");
            }
 });

The web method worked fine when I had a script manager on the page, but I want to remove the script manager and thought that using AJAX would be the best way.

Thanks

+1  A: 

I think if you change fail to error, you'll get the second alert box.

[Edit] I think if you then change

data: "{" + username + "}"

to

data: "{ 'username': '" + username + "' }"

you'll get the first alert, although it's hard to know that without seeing the service you're calling.

pdr
Hi, I changed it to error, and the alert does show. But I'm not sure why it's failing
o-logn
Ah, sorry didn't see your edit. That's fixed the problem! However, the web method returns a bool. How can I get this value from the 'success:' section (or from anywhere else in the call)?Thanks
o-logn
To get the data returned in a success, change the success:function() to success:function(data). Depending on your service setup, you will either get a data which is your boolean or a data with a property .d which is your boolean.
pdr
Bingo!Thanks a lot for the help. I used the '.d' property.
o-logn