2009-05-27 46 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 "}";