Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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
使用ASP MVC自动完成jQuery UI_Jquery_Asp.net Mvc_Jquery Ui_Jquery Autocomplete - Fatal编程技术网

使用ASP MVC自动完成jQuery UI

使用ASP MVC自动完成jQuery UI,jquery,asp.net-mvc,jquery-ui,jquery-autocomplete,Jquery,Asp.net Mvc,Jquery Ui,Jquery Autocomplete,我正试图让jQuery AutomcomComplete工作,但它不会像我想的那样:p 这是我的代码: JavaScript: $("#CustomerID").autocomplete({ source: function(request, response) { $.ajax({ type: "POST", url: "/customer/s

我正试图让jQuery AutomcomComplete工作,但它不会像我想的那样:p 这是我的代码:

JavaScript:

        $("#CustomerID").autocomplete({
            source: function(request, response) {
                $.ajax({
                    type: "POST",
                    url: "/customer/search",
                    dataType: "json",
                    data: {
                        term: request.term
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        alert('Error: ' + xhr.responseText);
                    },
                    success: function(data) {
                        response($.map(data, function(c) {
                            return {
                                label: c.Company,
                                value: c.ID
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function(event, ui) {
                alert('Select');
            }
        });
ASP MVC:

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult Search(string term)
    {
        if (term == null)
            term = "";

        List<JSON_Customer> customers = repCustomer.FindCustomers(term).ToList();
        return Json(customers);
    }

    public class JSON_Customer
    {
        public int ID { get; set; }
        public string Company { get; set; }
    }

    public IQueryable<JSON_Customer> FindCustomers(string searchText)
    {
        return from c in _db.Customers
               where c.Company.Contains(searchText)
               orderby c.Company
               select new JSON_Customer
               {
                   ID = c.ID,
                   Company = c.Company
               };
    }
我从$.ajax获得请求,并根据搜索词返回正确的客户列表。然后调用success方法。我可以看到数据有一个[object]值,但接下来我该怎么做?我的名单上没有客户。我正在使用响应$.map。。。源代码,但它就是不起作用


有人知道为什么吗?

我在第一次请求AJAX之前就使用了它,我打赌它会有所帮助。定义标准项,并将d属性作为顶级属性处理

  $.ajaxSetup({
     type: "POST",
     contentType: "application/json; charset=utf-8",
     data: "{}",
     dataFilter: function(data) {
        var msg;

        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
           msg = JSON.parse(data);
        else
           msg = eval('(' + data + ')');

        if (msg.hasOwnProperty('d'))
           return msg.d;
        else
           return msg;
     }
  });

你能使用诊断/调试工具,如Firebug插件用于FF、开发者工具F12用于IE或Ctrl+Shift+J用于Chrome,准确查看返回到浏览器的内容以及如何处理它吗?哇,谢谢!它成功了,现在一切都正常了。我必须删除contentType:application/json;但是charset=utf-8,因为当我使用它时,Searchstring术语中的术语为null。不客气。这最初是为了与ASP.NET 2.0一起使用而编写的-可能是较新的DLL不需要contentType。。。我不确定。