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

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Mr. D
  • 33
  • 1
  • 3
  • What is the actual code that `.autocomplete([ jsonArray_from_C# ]` generates? – Matt Ball Apr 27 '11 at 14:00
  • I'm using jQuery plugin http://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. – Mr. D Apr 27 '11 at 14:22
  • @Mr. D maybe you should use the jquery-ui-autocomplete as the bassintance one is deprecated, (http://jqueryui.com/demos/autocomplete/#remote). I don't see any problems on passing objects directly, you should really try it. – Gabriel Guimarães Apr 27 '11 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. – Mr. D Apr 27 '11 at 14:37
  • @Mr. D check this sample and see if it fits your use http://jqueryui.com/demos/autocomplete/#remote – Gabriel Guimarães Apr 27 '11 at 14:49
  • @Gabriel Guimarães I'm looking something like this but multiple auto complete in the same text box. For example once you choose an item and after you choose another item it should auto complete for the next item as well. Like I said before something related to StackOverflow tag. – Mr. D Apr 27 '11 at 14:58
  • @Gabriel Guimarães Sorry, I didn't realize there are more examples. Thank you for your help. I found the multiple remote example with jquery UI Autocomplete :) – Mr. D Apr 27 '11 at 15:04
  • @Mr. D your welcome, I just mentioned jquery UI because the one you mentioned is discontinued. That part of samples is really not easy to spot. – Gabriel Guimarães Apr 27 '11 at 16:39

1 Answers1

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.

Gabriel Guimarães
  • 2,724
  • 3
  • 27
  • 41