1

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;
}

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

3 Comments

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? :(
Well thanks alot if i added a msg.d.Name it worked like a charm, but must i allways add a .d. now? :P
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".
-1

You should return a string.

return "dc = {Id:"+dc.Id+", Name:" + dc.Name +", Description: " +dc.Description  + ", IsPublic: " +dc.IsPublic "}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.