Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass JSON array from c# to jQuery

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# ]

like image 517
Mr. D Avatar asked Jul 16 '26 18:07

Mr. D


1 Answers

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.

like image 66
Gabriel Guimarães Avatar answered Jul 18 '26 07:07

Gabriel Guimarães



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!