Search code examples
jqueryjquery-pluginsjquery-autocompletejquery-tokeninput

Server side textbox value is always empty string in case of webservice


I'm using tokenInput jquery plugin for autocomplete. This script is working fine

<script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/WebService1.asmx/HelloWorld7",
                data: "{}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) { $("#<%=demo.ClientID %>").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php");
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        });
    </script>

but when I replace the line

$("#<%=demo.ClientID %>").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php");

with

$("#<%=demo.ClientID %>").tokenInput(data.d);

it shows me autocomplete, but in button click event it shows demo.Text as empty string.I checked the response in firebug and the response is

{"d":[{"__type":"TestForMySite.fb","Id":1,"name":"ALABAMA"},{"__type":"TestForMySite.fb","Id":2,"name":"ALASKA"}]}

Solution

  • According to the documentation, your JSON Array is not in the correct format. It should be:

    [
        {"id":"856","name":"House"},
        {"id":"1035","name":"Desperate Housewives"},
        ...
    ]
    

    You have Id where it should be id.