2013-03-12 98 views
1

已解決:您無法從AJAX Web服務返回字典,請使用列表!ASMX Web服務不返回到jQuery

我想從jQuery調用ASP.NET Web服務。

Web服務運行,但結果不返回到JavaScript。任何想法爲什麼?我想我可能會錯過web.config或..中的東西?

的jQuery:

function GetAccountTypes() { 
     var ddl = $("#ListBoxType"); 
     clearSelect(ddl.attr("id")); 
     $.ajax({ 
      type: "POST", 
      url: "WebService.asmx/GetAccountTypes", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       alert(response.d); 
       var accTypes = response.d; 
       var options = ddl.attr("options"); 
       $.each(accTypes, function (index, accType) { 
        options[options.length] = new Option(accType.Value, accType.Key); 
       }); 
      }, 
      failure: function (msg) { 
       alert(msg); 
      } 
     }); 
    } 

Web服務:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 
public class WebService : System.Web.Services.WebService { 

public WebService() 
{ 
} 

[WebMethod] 
public Dictionary<int, string> GetAccountTypes() { 
    Dictionary<int, string> types = new ATDB().GetAccountTypes(); 
    return types; 
} 

} 

的web.config:

<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"/> 
    </system.web> 
</configuration> 

設置在js函數和一個破發點的Web服務方法表明,該方法運行,但永遠不會達到'成功'或'失敗'。

+3

Ajax程序員的最好朋友:Fiddler。用它來確定Web服務器的響應實際上是什麼。這會告訴你很多關於什麼可能是錯的。 http://www.fiddler2.com/fiddler2/ – 2013-03-12 00:55:25

+0

你的函數GetAccountTypes()等待一個json格式...你確定服務返回字典在json格式? – 2013-03-12 00:59:16

回答

2

好吧,我發現它實現IDoctionary而不是可序列化,你不能從一個Web服務返回一個字典。解決方案:返回一個簡單的自定義對象的列表。

+3

如果這解決了您的問題,您應該將自己的答案標記爲已接受。 – 2013-03-12 03:23:14