如何正确调用ajax中的web服务?

时间:2013-05-14 04:51:37

标签: c# jquery asp.net web-services

好吧,这就是我想要做的事情,我希望从活动目录中获取所有用户并将其放入列表中,因此我将从ajax调用Web服务,以获取所有用户并将其放入列表字符串,以后我想在文本框中使用jquery autocomplete,这是我以前用过的用户列表。

这就是我的所作所为:

  $(document).ready(function () {

            // Load data then initialize plugin:
            $.ajax({
                url: '/SvcADUser.asmx/GetADUserList',
                dataType: 'json'
            }).done(function (source) {

                var countriesArray = $.map(source, function (value) { return { value: value }; }),
                    countries = $.map(source, function (value) { return value; });

                // Setup jQuery ajax mock:
                $.mockjax({
                    url: '*',
                    responseTime: 200,
                    response: function (settings) {
                        var query = settings.data.query,
                            queryLowerCase = query.toLowerCase(),
                            suggestions = $.grep(countries, function (country) {
                                return country.toLowerCase().indexOf(queryLowerCase) !== -1;
                            }),
                            response = {
                                query: query,
                                suggestions: suggestions
                            };

                        this.responseText = JSON.stringify(response);
                    }
                });



                // Initialize autocomplete with local lookup:
                $('#MainCT_dtvJobVac_PIC').autocomplete({
                    lookup: countriesArray,
                    onSelect: function (suggestion) {
                        $('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
                    }
                });

            });

        }());

    }());

但这会给我一个错误,"NetworkError: 500 Internal Server Error - http://localhost:60525/SvcADUser.asmx/GetADUserList",如果我将网址更改为SvcADUser.asmx,它不会出错,但不会给我任何结果。

我在这里做错了什么?顺便说一下这是我的网络服务代码:

 [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class SvcADUser : System.Web.Services.WebService
    {
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public List<string> GetADUserList(string keyword)
        {
            List<string> alluser = new List<string>();

            using (var context = new PrincipalContext(ContextType.Domain, "weekendinc.com"))
            {
                using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
                {
                    foreach (var result in searcher.FindAll())
                    {
                        DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;

                        alluser.Add((string)de.Properties["samAccountName"].Value);
                    }
                }
            }

            var filtereduser = alluser.Where(usr => usr.ToLower().StartsWith(keyword.ToLower()));

            return filtereduser.ToList();
        }

    }

1 个答案:

答案 0 :(得分:0)

试试这个

jQuery.ajax({
            type: 'POST',
            contentType: 'application/json;',
            data: '{keyword:"test"}',
            dataType: 'json',
            async: true,
            url: 'SvcADUser.asmx/GetADUserList',
            success: function (result) {
                alert(result.d);
            }
        });