开发者

Converting an array to JSON and passing that to asmx

开发者 https://www.devze.com 2022-12-28 18:36 出处:网络
I am trying to use JSON.stringify() (from json2.js of json[dot]org ) to convert a JavaScript array to JSON string and to pass it to an asmx web method. I use jQuery AJAX.

I am trying to use JSON.stringify() (from json2.js of json[dot]org ) to convert a JavaScript array to JSON string and to pass it to an asmx web method. I use jQuery AJAX.

The call reaches the web method where I take a List <Object> as parameter but I get an empty list there in debug mode.

My JSON string looks like well formed with all data , I even tr开发者_运维知识库ied having single-quotes and double-quotes(escaped) around the 'names' of the JSON string. Please help.


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

Will accept a JSON string that looks like this:

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

So , try something like this:

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


http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx

This link helped me out as well because I was looking to use a model for bringing the json data.

As an additional note, I was trying to use $.post(...), but I was not having any luck until I broke it out to a $.ajax call and specified the content-type.


I have found the solution to your problem

Solution 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消