I am getting "401 - Unauthorised" error in jQuery Autocomplete control in MVC3 application (.net framework v4.0.30319) which is deployed on a server containing IIS 7.5
Controller: (SearchSuggestController)
[HttpGet]
public JsonResult SuggestByEmployeeId(string Id)
{
var suggestions = from a in Context.Employees
where a.EmployeeId.Contains(Id)
select new
{
a.EmployeeId,
a.FirstName,
a.LastName
};
return Json(suggestions, JsonRequestBehavior.AllowGet);
}
jQuery: (Autocomplete)
$(function () {
$("#IDFilter").autocomplete({source: function (request, response) {
$.ajax({
url: "/SearchSuggest/SuggestByEmployeeId",
type: "POST",
dataType: "json",
data: { Id: request.term },
error: function (XMLHttpRequest, status, error) {
alert("Error status:(" + status + ") error:(" + error + ")");
},
success: function (data) {
response($.map(data, function (item) {
return { label: item.EmployeeId, value: item.EmployeeId,id: item.EmployeeId };
}));
}
});
},
minLength: 3,
autoFocus: true,
select: function (event, ui) {
$("#IDFilter").val(ui.item.value);
}
});});
jQuery is calling url: "/SearchSuggest/SuggestByEmployeeId" i.e. SearchSuggest controller's SuggestByEmployeeId action.
Search.cshtml:(View)
@using (Html.BeginForm("BasicSearch", "Employee"))
{
@Html.Label("Employee Id:")
@Html.TextBox("IDFilter")
<input type="submit" value="Search" />
}
BasicSearch action method in Employee controller is working fine if valid EmployeeID is entered in "IDFilter" and clicked on Search button.
Autocomplete is showing expected results when i run the code through visual studio IDE. After publishing and Bin Deploying of this code to IIS 7.5 is throwing 401-Unauthorized error. The application is hosted under Default Web Site in IIS.
Anybody has any idea what is going wrong here?