2013-05-01 25 views
0

由于我是新来淘汰赛,Web服务和所有这些东西,我试图用knockout js和web服务填充下拉列表。使用web服务序列化json响应

的HTML代码

<body> 
    <select data-bind="options: printers"></select> 
</body> 

和JavaScript块

<script> 
    $(document).ready(function() { 
     var viewModel = { 
      printer: ko.observable(), 
      printers: ko.observableArray() 
     } 
     $.ajax({ 
      type: "POST", 
      contentType: "application/json", 
      url: "PapersDDLs.asmx/getPrinters1", 
      data: "{}", 
      dataType: "json", 
      success: function (response) { 
       viewModel.printers(response.d); 
      } 
     }); 

     ko.applyBindings(viewModel); 
    }); 
</script> 

Web服务我打电话是

Imports System.Web 
Imports System.Web.Services 
Imports System.Web.Services.Protocols 
Imports System.Collections 
Imports System.Collections.Generic 
Imports System.Collections.Specialized 
Imports System.Web.Script.Serialization 

<System.Web.Script.Services.ScriptService()> _ 
<WebService(Namespace:="http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Public Class PapersDDLs 
    Inherits System.Web.Services.WebService 

    <WebMethod()> _ 
    Public Function getPrinters1() As String 
     Dim db As New DataClassesDataContext 
     Dim printers = From p In db.Printers Select p 
     Dim values As New List(Of PrinterItem) 
     For Each pr In printers 
      values.Add(New PrinterItem(pr.BrandModelName, pr.Id.ToString())) 
     Next 
     db.Dispose() 
     Return New JavaScriptSerializer().Serialize(values) 
    End Function 

    End Class 

的问题是,返回的字符串是由人物造字。

任何帮助将有价值

谢谢!

+0

因此,这是不工作?你在JavaScript控制台中收到任何错误? – PatrickSteele 2013-05-01 14:28:58

回答

0

Web服务中的功能稍微不正确。你不需要自己做Javascript序列化。由于您已将Web服务标记为System.Web.Script.Services.ScriptService,因此响应的内容将自动序列化为JSON。

方法签名应该是:

Public Function getPrinters1() As List(Of PrinterItem) 

和return语句应该是:

Return values