我執行引導typeahead.js並試圖利用像這樣一個簡單的AJAX GET請求:AJAX GET請求命中控制器,但不會去URL在瀏覽器
$("#find-company-search").typeahead({
source: function (query, process) {
var companies = [];
map = {};
// This is going to make an HTTP post request to the controller
return $.post('/Companies/GetCompanies', { query: query }, function (data) {
//console.log(data);
// Loop through and push to the array
$.each(data, function (i, company) {
console.log(company);
console.log(company.CompanyID);
// Use the Name field on the display and for the map key.
map[company.Name] = company;
companies.push(company.Name);
});
// Process the details
process(companies);
});
},
updater: function (item) {
console.log(item);
console.log(map[item]);
console.log(map[item].CompanyID);
var selected = map[item].CompanyID;
// Put ajax code to load the company here.
$.ajax({
url: "/companies/?companyID=" + selected,
success: function() {
alert("Success!");
}
});
return item;
}
});
當我提出請求,它擊中以下控制器的操作方法:
public ActionResult Index() {
if (CurrentUser.IsCompany(User)) {
// We need to cast to a Company list
return View(new List<Company> { db.Companies.Find(CurrentUser.CompanyID(User)) });
}
var filters = new Dictionary<string, int>();
var companyID = 0;
Int32.TryParse(Request.Params["companyID"], out companyID);
filters["companyID"] = companyID;
filters["hasTickets"] = Convert.ToInt32(Request.Params["has-tickets"] == "on");
filters["hasDevices"] = Convert.ToInt32(Request.Params["has-devices"] == "on");
filters["isCustomer"] = Convert.ToInt32(Request.Params["type"] == "customer");
filters["isVendor"] = Convert.ToInt32(Request.Params["type"] == "vendor");
filters["isLead"] = Convert.ToInt32(Request.Params["type"] == "lead");
ViewBag.Filters = filters;
var companies = db.Companies.Where(c => c.Hidden != true);
ViewBag.CompaniesForDropdown = companies.ToList();
if (filters.Count(filter => filter.Value <= 0) == filters.Count()) {
// Return nothing if we haven't filtered.
return View(new List<Company>());
}
foreach (var filter in filters.Where(filter => filter.Value > 0)) {
// <= 0 means false, or no selected company, so ignore those.
switch (filter.Key) {
case "companyID":
companies = companies.Where(c => c.CompanyID == companyID);
break;
case "hasTickets":
companies = companies.Where(c => c.ServiceTickets.Count > 0);
break;
case "hasDevices":
companies = companies.Where(c => c.Devices.Count > 0);
break;
case "isCustomer":
companies =
companies.Where(c => c.Company_CompanyType.Count(t => t.CompanyType.Name == "Customer") > 0);
break;
case "isVendor":
companies =
companies.Where(c => c.Company_CompanyType.Count(t => t.CompanyType.Name == "Vendor") > 0);
break;
case "isLead":
companies = companies.Where(c => c.Company_CompanyType.Count(t => t.CompanyType.Name == "Lead") > 0);
break;
}
}
return View(companies.OrderBy(c => c.Name).ToList());
}
當我提交輸入的文本我的引導預輸入,它擊中的控制器,並填充爲預期companyID
變量,但頁面不執行任何操作。但是,如果我手動導航到瀏覽器中的URL(例如/ companies /?companyID = 1604),則我的所有數據都會按預期顯示。我的AJAX請求有問題嗎?
編輯:我應該指定AJAX請求是問題,那就是:
$.ajax({
url: "/companies/?companyID=" + selected,
success: function() {
alert("Success!");
}
});
你只有一個回調你的ajax請求,成功回調。如果您還提供失敗回調,您可能會發現一些有用的東西。 – 2014-10-27 16:25:47
@RaphaelSerota我已經添加了一個錯誤回調,它從來沒有擊中它。但是,它顯示我的成功警報。 – mdk09 2014-10-27 16:32:38
在您的成功回調中,返回的數據可以作爲參數引用。請記錄這一點,並檢查,看看你回來。另外,如Raphael所說,添加並記錄其他處理程序。 – Ngz 2014-10-27 16:32:39