views:

75

answers:

2
+1  Q: 

JQuery and ASP.Net

Well im trying to return a more complex type than a string or bool but i fail what am i doing wrong? JavaScript

<script language="javascript" type="text/javascript">
    ///<Reference Path="~/Script/jquery-1.3.2-vsdoc.js" />
    $(document).ready(function() {
        // Add the page method call as an onclick handler for the div.
        $("#Result").click(function() {
            $.ajax({
                type: "POST",
                url: "Test.aspx/GetDate",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.Name);
                },
                failure: function() { alert("Failed") }
            });
        });
    });
</script>

C# (This is not a webservice just a normal webpage)

[WebMethod]
public static ImageDC GetDate()
{
    ImageDC dc = new ImageDC();
    dc.Id = 1;
    dc.Name = "Failwhale";
    dc.Description = "Hurry the failwale is going to eat us!";
    dc.IsPublic = true;
    return dc;
}
A: 

You should return a string.

return "dc = {Id:"+dc.Id+", Name:" + dc.Name +", Description: " +dc.Description  + ", IsPublic: " +dc.IsPublic "}";
Daniel Moura
+2  A: 

I'm not sure what version .NET your running, but there is a breaking change with object returned from a web service. Check out this article.

http://encosia.com/2009/02/10/a-breaking-change-between-versions-of-aspnet-ajax/

If you use fiddler to look at the request/response, it should be easy to tell if this is the problem.

http://www.fiddler2.com/fiddler2/

Zachary
So i must create a webservce? it seems lame if i want to make a webpage to be forced to create a webservice just to juse ajax? :(
Petoj
Im running .net 3.5
Petoj
Well thanks alot if i added a msg.d.Name it worked like a charm, but must i allways add a .d. now? :P
Petoj
Yes, this is a requirement. Always add the ".d" when you return an object from a web service. I read some notes about security issues that this protects you against, so this is considered a "good change".
Zachary