Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用asp WebMethod中的数据填充jquery自动完成_Javascript_Jquery_Jquery Ui_Jquery Ui Autocomplete - Fatal编程技术网

Javascript 使用asp WebMethod中的数据填充jquery自动完成

Javascript 使用asp WebMethod中的数据填充jquery自动完成,javascript,jquery,jquery-ui,jquery-ui-autocomplete,Javascript,Jquery,Jquery Ui,Jquery Ui Autocomplete,我的asp.net页面中有一个WebMethod,它返回一个字符串,该字符串表示JSON数组,其中包含我要用作jquery ui自动完成文本框源的数据 [WebMethod] public static string GetCities(string cityName) { JArray json = new JArray( new JObject(new JProperty("label", "label1"), new JProperty("category", "c

我的asp.net页面中有一个WebMethod,它返回一个字符串,该字符串表示JSON数组,其中包含我要用作jquery ui自动完成文本框源的数据

[WebMethod]
public static string GetCities(string cityName)
{
    JArray json = new JArray(
        new JObject(new JProperty("label", "label1"), new JProperty("category", "cat1"), new JProperty("value", "v1")),
        new JObject(new JProperty("label", "label2"), new JProperty("category", "cat1"), new JProperty("value", "v2")),
        new JObject(new JProperty("label", "label3"), new JProperty("category", "cat2"), new JProperty("value", "v3")),
        new JObject(new JProperty("label", "label4"), new JProperty("category", "cat3"), new JProperty("value", "v4")));
    return json.ToString();
}
javascript代码:

<script type="text/javascript">
    $(function () {
        $("#txtCity").autocomplete({
            source: function (request, response) {
                var param = { cityName: $('#txtCity').val() };
                $.ajax({
                    url: "WebForm1.aspx/GetCities",
                    data: JSON.stringify(param),
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        response(data.d); //here data.d contains the json array string
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            select: function( event, ui ) {
                $( "#output" ).val( ui.item.value );
                return false;
            },
            minLength: 2//minLength as 2, it means when ever user enter 2 character in TextBox the AutoComplete method will fire and get its source data. 
        });
    });
</script>

$(函数(){
$(“#txtCity”).autocomplete({
来源:功能(请求、响应){
var param={cityName:$('#txtCity').val()};
$.ajax({
url:“WebForm1.aspx/GetCities”,
数据:JSON.stringify(param),
数据类型:“json”,
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
dataFilter:函数(数据){返回数据;},
成功:功能(数据){
response(data.d);//这里data.d包含json数组字符串
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
警报(文本状态);
}
});
},
选择:功能(事件、用户界面){
$(“#输出”).val(ui.item.value);
返回false;
},
minLength:2//minLength为2,这意味着当用户在文本框中输入2个字符时,自动完成方法将触发并获取其源数据。
});
});
但是我的自动完成为JSON字符串中的每个字符生成一个项。我想我没有正确返回它。

找到了它

success: function (data) {
                        response($.parseJSON(data.d))
                    },
必须解析JSON

找到了答案

success: function (data) {
                        response($.parseJSON(data.d))
                    },
必须解析JSON