Pass JSON array from c# to jQuery

夙愿已清 提交于 2019-12-23 21:08:08

问题


I'm doing jQuery autocomplete. Works fine if I put hard codded JSON array. But it fails when I pass the array from c#. Please help, I spend enough time with it and I'm stuck!

Here is my jQuery code in AutoComplete.aspx

<script type="text/javascript">
    $(document).ready(function () {
        var msgbox = $("#status");
        $.ajax({
            type: "POST",

            //Page Name (in which the method should be called) and method name
            url: "AutoControl.aspx/GetData",

            //else If you don't want to pass any value to server side function leave the data to blank line below
            data: "{}",

            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function (msg) {
                $("#status").val(msg.d);
            }
        });

        $('#<%=tags.ClientID%>').autocomplete(["c++", "java", "php", "coldfusion"], {
            width: 320,
            max: 4,
            highlight: false,
            multiple: true,
            multipleSeparator: " ",
            scroll: true,
            scrollHeight: 300
        });
    });        

</script>

Here is my C# code in AutoComplete.aspx.cs

[System.Web.Services.WebMethod]
    public static string GetData()
    {
        return "\"c++\", \"java\", \"php\"";
    }

How do I pass the JSON array from C# to jQuery. With this code I could retrieve the values from c# but some reason JSON is not reading the values.

I want to change this code: $('#<%=tags.ClientID%>').autocomplete(["c++", "java", "php", "coldfusion"]

to

$('#<%=tags.ClientID%>').autocomplete([ jsonArray_from_C# ]


回答1:


Have you tried returning an string array?

http://encosia.com/2011/04/13/asp-net-web-services-mistake-manual-json-serialization/

don't try to parse Json, pass the object directly.



来源:https://stackoverflow.com/questions/5805316/pass-json-array-from-c-sharp-to-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!