2010-05-03 58 views
2

我試圖使用JSON.stringify()(來自json [dot] org的json2.js)將JavaScript數組轉換爲JSON字符串並將其傳遞給asmx Web方法。我使用jQuery AJAX。將數組轉換爲JSON並將其傳遞給asmx

呼叫到達網絡方法,我以列表<Object>作爲參數,但我在調試模式下得到一個列表。我的JSON字符串看起來很像所有的數據,我甚至嘗試過在JSON字符串的'名稱'周圍使用單引號和雙引號(轉義)。請幫忙。

回答

5
[WebMethod] 
public void SomeMethod(List<object> param) 
{ 
.... 
} 

會接受一個JSON字符串,它看起來像這樣:

'{"param": ["xx", "zz", "yy"]}' 

因此,嘗試這樣的事情:

var data = JSON.stringify({param: myarray}); 
+0

就是這樣:-)。我在做 - JSON.stringify(myArr); 但它包裹在成員'd'中。所以,這工作 - JSON.stringify(myArr.d); – user96403 2010-05-03 13:09:07

0

我已經找到了解決您的問題

解決方案prashiddha.com.np

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

      <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head runat="server"> 
      <title></title> 

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> 
      <script type="text/javascript"> 
       var url = '<%=ResolveUrl("~/WebService.asmx/HelloWorld")%>'; 
       $(document).ready(function() { 
        $('#txtAutoSuggest').keyup(function() { 
        var str = $("#txtAutoSuggest").val(); 
        var a = JSON.stringify({ name: str }); 
        CallService(a); 
       }); 
      }); 

      function CallService(a) { 
      $.ajax({ 
       type: "POST", 
       url: url, 
       data: a, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function(data, status) { 
       $('#lblResult').text(data.d); 
      }, 
      error: Error 
      }); 
      } 

      function Error(request, status, error) { 
       $('#lblResult').text("Not Matched"); 
      } 
      </script> 
      </head> 
      <body> 
      <form id="form1" runat="server"> 
      <div> 
      <asp:TextBox ID="txtAutoSuggest" runat="server"></asp:TextBox> 
      <asp:Label ID="lblResult" Text=" " Width="100%" runat="server" /> 
      </div> 
      </form> 
      </body> 
      </html> 
相關問題