jQuery自动完成不使用JSON

时间:2017-08-06 00:17:58

标签: javascript c# jquery asp.net asp.net-mvc

我正在尝试实现jQuery自动完成功能。我觉得一切都设置得很好,但它没有用。

SearchSkies方法

li#uploadMenu:after {
    content: "\2699 \00a0 SUPPORT ";
}

视图中的脚本

  public JsonResult SearchSkies(string query)
        {
            SkiDao skiDao = new SkiDao();
            IList<Ski> skies = skiDao.SearchSkies(query);

            List<string> brands = (from Ski s in skies select s.Brand).ToList();
            return Json(brands, JsonRequestBehavior.AllowGet);
        }

1 个答案:

答案 0 :(得分:0)

您没有在ajax调用中提及请求类型(GET / POST)。

$(document).ready(function () {  
       $("#searchBox").autocomplete({  
           source: function(request,response) {  
               $.ajax({  
                   url: "/Skies/SearchSkies",  
                   type: "POST",    <<----
                   dataType: "json",  
                   data: { query: request.term },  
                   success: function (data) {  
                       response($.map(data, function (item) {  
                           return { label: item.Name, value: item.Name };  
                       }))      
                   }  
               })  
           },  
           messages: {  
               noResults: "", results: ""  
           }  
       });  
   })

和控制器

public class SkiesController : Controller  
{  
        // GET: Home  
        [HttpGet]  
        public ActionResult Index()  
        {  
            return View();  
        }  
        [HttpPost]  
        public JsonResult SearchSkies(string query)  
        {                
        SkiDao skiDao = new SkiDao();
        IList<Ski> skies = skiDao.SearchSkies(query);

        List<string> brands = (from Ski s in skies select s.Brand).ToList();
        return Json(brands, JsonRequestBehavior.AllowGet);
        }  
 }