2009-05-27 26 views
1

嗯即时尝试返回比字符串或布尔型更复杂的类型,但我失败了我做错了什么? 的JavaScriptJQuery和ASP.Net

<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#(这不是一个web服务只是一个正常的网页)

[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

我不确定什么版本的.NET运行,但是有一个从Web服务返回的对象发生了重大变化。看看这篇文章。

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

如果您使用Fiddler查看请求/响应,它应该很容易分辨这就是问题所在。

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

+0

所以我必须创建一个webservce?它似乎跛脚,如果我想让网页被迫创建一个web服务只是为了juse ajax? :( – Peter 2009-05-27 21:56:18

-1

你应该返回一个字符串。

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