2010-10-30 65 views
1

這裏是我的web服務:得到web服務數據

public class Header : System.Web.Services.WebService { 
    public Header() {} 
    [WebMethod] 
    public string GetArchive(string PageID) 
    { 
     StringBuilder sb = new StringBuilder(); 
     BusinessRules.News news = new BusinessRules.News(); 
     BusinessObject.NewsItemList newsList = 
      news.GetListbySectionID(int.Parse(PageID)); 

     foreach (BusinessObject.NewsItem item in newsList) 
     { 
      sb.Append(item.ID + " : " + item.Date); 
     } 
     return sb.ToString(); 
    } 

} 

其中

<body> 
    <form id="form1" runat="server"> 
    <div>   
     <div runat="server" id="Content"> 
     </div> 
     <div> 
      <a id="LinkBtnAll" href="#">View</a> 
     </div> 
    </div> 
    </form> 
</body> 

<script type="text/javascript"> 
    $(document).ready(function() { 

     var ParamValue = $.getUrlVar("id");   

     $('#LinkBtnAll').click(function() { 
      $.ajax({ type: "POST", 
       url: "Services/Header.asmx/GetArchive", 
       data: "{'PageID'," + ParamValue + "}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       async: true, 
       cache: false, 
       success: function (msg) { 
        $('#Content').text(msg.d); 
       } 
      }) 
      return false; 
     }); 
    });    
</script> 

它不工作是任何人誰可以幫我?

回答

1

首先爲web方法應該使用[ScriptMethod (ResponseFormat = ResponseFormat.Json)]屬性或在web.config中配置相同的如果您使用.NET 4.0。在這兩種情況下,您都不需要手動推動任何類型的JSON序列化。而不是你可以返回對象本身。換句話說,你應該遵循的建議tvanfosson,但返回List<Article>直接:

[System.Web.Script.Services.ScriptService] 
public class Header : System.Web.Services.WebService { 
    [WebMethod, ScriptMethod (ResponseFormat = ResponseFormat.Json)] 
    public List<Article> GetArchive(int PageID) 
    { 
     BusinessRules.News news = new BusinessRules.News(); 
     BusinessObject.NewsItemList newsList = news.GetListbySectionID(PageID)); 
     return newsList.Select (a => new Article 
         { 
          ID = a.ID, 
          Date = a.Date 
         }); 
    } 

    public class Article 
    { 
     public string ID { get; set; } 
     public string Date { get; set; } 
    } 
} 

我改變了輸入參數PageID的類型從stringint表明輸入參數不能是隻有字符串。它也可以是類實例(請參見my old another)。

在客戶端的$(document).ready(function() {/*the code here you will see below*/});內:

var ParamValue = $.getUrlVar("id"); 
var pageID = parseInt(ParamValue,10); 

$('#LinkBtnAll').click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "Services/Header.asmx/GetArchive", 
     data: {PageID: JSON.stringify(pageID)}, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      $('#Content').text(msg.d); 
     } 
    }) 
    return false; 
}); 

msg.d的結果,你應該在其他的方式實現,因爲msg.d是一個數組,但它是一個簡單的JavaScript代碼的解碼。

我建議您查看old answeranother one瞭解更多信息。

0

你的JSON語法是錯誤的,一個。整個事情需要用大括號括起來,你的屬性需要用逗號分隔,並且你應該引用你的屬性名稱和可能的值,因爲值可能有空格。

public class Header : System.Web.Services.WebService 
{ 
    public Header() {} 

    [WebMethod] 
    public string GetArchive(string PageID) 
    { 
     StringBuilder sb = new StringBuilder("{"); 
     BusinessRules.News news = new BusinessRules.News(); 
     BusinessObject.NewsItemList newsList = news.GetListbySectionID(int.Parse(PageID)); 
     foreach (BusinessObject.NewsItem item in newsList) 
     { 
      sb.Append("\"" + item.ID + "\" : \"" + item.Date + "\","); 
     } 
     return sb.ToString().TrimEnd(",") + "}"; 
    } 
} 

話雖如此,更好的方法是使用JSON序列化程序。請注意,以下示例中的格式將與JSON對象的集合不同,而不是包含所有屬性的單個JSON對象。您必須更改您的客戶端代碼才能解決此問題。

public class Header : System.Web.Services.WebService 
{ 
    private class Article 
    { 
     public string ID { get; set; } 
     public DateTime Date { get; set; } 
    } 

    public Header() {} 

    [WebMethod] 
    public string GetArchive(string PageID) 
    { 
     var articles = new List<Article>(); 
     BusinessRules.News news = new BusinessRules.News(); 
     BusinessObject.NewsItemList newsList = news.GetListbySectionID(int.Parse(PageID)); 
     var articles = newsList.Select(a => new Article 
         { 
          ID = a.ID, 
          Date = a.Date 
         }; 
     var stream = new MemoryStream(); 
     using (var serializer = new DataContractJsonSerializer(articles.GetType()); 
     serializer.WriteObject(stream, articles); 
     return Encoding.Default.GetString(stream.ToArray()); 
    } 
}