1

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?

Mayank Jha
  • 371
  • 1
  • 4
  • 10

3 Answers3

1

To avoid issues with relative paths, rather than hard-coding the URL, use the @URL.Action helper.

So, your old code: url: "/SearchSuggest/SuggestByEmployeeId",
should become: url: '@Url.Action("SuggestByEmployeeId", "SearchSuggest")'

Good luck!

Eric
  • 139
  • 2
  • 11
1

You are AJAX POST to the action, but your action only accepts GET ?

Does your controller (or base controller) have the [Authorize] attribute?

K. Bob
  • 2,668
  • 1
  • 18
  • 16
  • My mistake, I have updated jQuery to AJAX GET. [Authorize] attribute is not applied on any of the controller. Issue still persist. – Mayank Jha Dec 16 '11 at 05:02
1

I think i have got a solution.

Digging into jQuery using IE8 Developer Tools

On host server, the autocomplete XMLHttpRequest is trying to search for Request URL "http://localhost:80/SearchSuggest/SuggestByEmployeeId?Id=123"

But my application is hosted as "MyApp" under Default Web Site, so the URL should be like "http://localhost:80/MyApp/SearchSuggest/SuggestByEmployeeId?Id=123"

Hence i have updated the URL to url: "/MyApp/SearchSuggest/SuggestByEmployeeId"

After this change Autocomplete is fetching the expected results. 401 error is gone.

Probably in host environment, i will have to setup a new web site and bin deploy my MVC application under that. This way i don't need to make this modification in jQuery everytime.

If anyone has got a better solution, please suggest.

Mayank Jha
  • 371
  • 1
  • 4
  • 10