views:

37

answers:

1

I am simply learing Ajax with jQuery and have a simple page method that takes in a parameter and returns a string. For some reason, I am not getting the return string but I am getting to my 'success' message:

$(document).ready(function() {
            $.ajax({
                type: "POST",
                url: "testFormMatt.aspx/sayHello",
                contentType: "application/json; charset=utf-8",
                data: '{"name": "matt"}',
                dataType: "json",
                success: function(msg) {
                    $.jGrowl('The web service has been successfully called');
                    $('#result').append(msg);
                }
            });
});
+4  A: 

When you call append, you need to specify the property of the JSON object that you want to append.

So if your page is returning:

{ message: "Hello, Matt" }

Then you'd need to call append like this:

$("#result").append(msg.message);

If your page is not returning JSON, then you need to take the dataType: "json" out of the $.ajax call. The dataType parameter is for specifying the expected data type of the response, not the data type of the request.

Lobstrosity
Ok, cool. Didn't know that about the dataType: "json". I am now getting this back in my alert. {"d":"hello matt."}How do I clean that up?
Matt
Your result is JSON so you need to leave 'dataType: "json"' in there. If your property is called d, you'd want to call $("#result").append(msg.d);
Lobstrosity