2013-03-12 99 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