在为jquery自动完成设置源代码时,如何将JSON数据传递给WCF方法

在为jquery自动完成设置源代码时,如何将JSON数据传递给WCF方法,jquery,wcf,jquery-ui,autocomplete,Jquery,Wcf,Jquery Ui,Autocomplete,我在stackoverflow上看到了很多类似的帖子,但都不适合我。我有一个简单的支持AJAX的WCF服务,它根据输入返回城市 [OperationContract] public IEnumerable<string> GetCities(string prefix) { string[] cities = new[] { "New York", "Atlanta", "Los Angeles", "Las Vegas", "Arizona", "

我在stackoverflow上看到了很多类似的帖子,但都不适合我。我有一个简单的支持AJAX的WCF服务,它根据输入返回城市

 [OperationContract]
    public IEnumerable<string> GetCities(string prefix)
    {
        string[] cities = new[] { "New York", "Atlanta", "Los Angeles", "Las Vegas", "Arizona", "New Hampshire", "New England" };

        if(!String.IsNullOrEmpty(prefix))
        {
            cities = cities.Where(a => a.ToLower().StartsWith(prefix.ToLower())).ToArray();
        }

        return cities;
    }
我知道我有3个选项可以调用远程数据源。 第一个选项是上面的代码,但通过这种方式,我将字符串化json结果附加到url

然后我试图简单地将服务的url传递给源选项,但得到了相同的结果(url后面附加了term=myinput)

我不能使用第三个选项(带有本地数据的数组),因为我有很多条目,我不想把它们都保存在客户端上


那个么,如何将前缀参数传递给服务方法呢?这是可能的,或者如果我选择使用jQueryUI的自动完成小部件,我必须使用url附加的参数吗?

我没有看到ajax调用中指定的HTTP方法(
类型
参数),所以它默认为GET,GET不能有内容。使用GET时传递数据的唯一方法是通过URL参数。顺便说一句,因为您的方法只是获取数据,所以它应该使用GET请求

因此,不发送JSON,只发送:

var dataCity = "prefix=" +  encodeURIComponent(request.term);

事实上,我在代码中犯了一些错误。我应该映射返回的结果,在本例中是data.d。这是工作代码

$("input[type=text][id*=autocompleteBox]").autocomplete({

        source: function (request, response) {

            var data = { 'prefix': request.term};

            $.ajax({
                type: "POST",
                url: "http://localhost:1939/Cities.svc/GetCities",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(data),
                processdata: true,
                success: function (result) {
                    response($.map(result.d, function (item) {
                        return {
                            label: item
                        };
                    }));
                },
                error: function (er) {
                    alert(er);
                }
            });
        },
        minLength: 1,
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });

您好,感谢您的快速响应,我不同意使用GET请求获取数据的想法。实际上,我在代码中犯了一些错误,特别是我应该将data.d映射为而不是data。我将发布完整的代码作为答案。Sry忘了提及,感谢您指出我没有指定类型参数。
$("input[type=text][id*=autocompleteBox]").autocomplete({

        source: function (request, response) {

            var data = { 'prefix': request.term};

            $.ajax({
                type: "POST",
                url: "http://localhost:1939/Cities.svc/GetCities",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(data),
                processdata: true,
                success: function (result) {
                    response($.map(result.d, function (item) {
                        return {
                            label: item
                        };
                    }));
                },
                error: function (er) {
                    alert(er);
                }
            });
        },
        minLength: 1,
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });