3

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

8
  • What is the actual code that .autocomplete([ jsonArray_from_C# ] generates? Commented Apr 27, 2011 at 14:00
  • I'm using jQuery plugin bassistance.de/jquery-plugins/jquery-plugin-autocomplete it basically looking for a JSON array. I'm not sure if I can pass object with this plugin. Commented Apr 27, 2011 at 14:22
  • @Mr. D maybe you should use the jquery-ui-autocomplete as the bassintance one is deprecated, (jqueryui.com/demos/autocomplete/#remote). I don't see any problems on passing objects directly, you should really try it. Commented Apr 27, 2011 at 14:30
  • @Gabriel Guimarães I want to do multiple auto complete. For example I'm doing a tagging system like StackOverflow. I have no issue with the script that I'm using, I wonder if the jquery UI would allow to do multiple auto complete. Commented Apr 27, 2011 at 14:37
  • @Mr. D check this sample and see if it fits your use jqueryui.com/demos/autocomplete/#remote Commented Apr 27, 2011 at 14:49

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

provided link is broken

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.