开发者

ASP.Net - Ajax Autocomplete using jQuery

Hi

I have been trying to implement auto complete in my site from 2 hrs and still couldn't get thru.

Here's my code.

<script type="text/jscript">
        $(document).ready(function() {

    $.ajax({
        type: "POST",
        url: "/AjaxLoad.asmx/GetBrands",
        dataType: "json",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            var datafromServer = data.d.split(":");
            $("[id$='tbBrands']").autocomplete({
                source: datafromServer
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
           alert(textStatus);
        }
    });

}
    </script>

    <div id="ajaxbrands">
        <input id="tbBrands" runat="server" />
    </div>

Web service code

开发者_如何学Go
[WebMethod]
    public string GetBrands()
    {
        StringBuilder sbStates = new StringBuilder();

        sbStates.Append("Apple").Append(":");
        sbStates.Append("Apex").Append(":");
        sbStates.Append("Amex").Append(":");
        sbStates.Append("Unity").Append(":");
        sbStates.Append("Unex").Append(":");
        sbStates.Append("Unitel");            
        return sbStates.ToString();
    }

GetBrands method returns simple string in response with ":" as delimiter. Could someone point me in right direction!

Update: I put a break point in Web service code but it was not hit! Do you think there is problem with the way I am calling the web service!


json string should be like this

{"key":"value","key1":"value1"}

and use

success: function(data) {

alert($.parseJSON(data));
            var datafromServer = data.d.split(":");
            $("[id$='tbBrands']").autocomplete({
                source: datafromServer
            });
        },





for (var key in result) {
    if (result.hasOwnProperty(key)) {
         alert(result[key]);


You're using the dataType: "json" which means jquery will try to evaluate the result as JSON, if you are using plain text for the response use dataType: "text".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜