views:

277

answers:

2

I am unable to access an action in my controller using .ajax. The code works on my development machine but as soon as I place it on the server it gives the error 401 Unauthorized. Here is a snippet of the code in the .aspx file...

var encoded = $.toJSON(courseItem);


    $.ajax({
        url: '<%= Url.Action("ViewCourseByID", "Home") %>/',
        type: "POST",                        
        dataType: 'json',
        data: encoded,
        //contentType: "application/json; charset=utf-8",
        success: function(result) {

Update: The only time this doesn't work is when I pass json data to the Ajax call, it works fine with HTML data.

A: 

This doesn't have anything to do with ajax. get the URL that you are requesting and paste it into a separate browser windows and you will likely find that it will give you a 401.

So the answer is to look into why you're getting the 401 (iis configuration, action authorize attribute, etc.)

Joel Martinez
What is the proper configuration for IIS7?
GB
A: 

Answer: It looks like i found my problem.

In my Controller I have the following method:

[JsonFilter(Param = "course", JsonDataType = typeof(EmployeeSearchItem))]
public ActionResult ViewCourseByID(EmployeeSearchItem course)
{ 

...}

And EmployeeSearchItem is a class in its own .cs file:

 namespace EducationAssistanceMVC.Models
{
public class EmployeeSearchItem //: Controller

{

public string empIDSearch { get; set; }

...}

My solution was to comment out the inheritance of Controller above in the EmployeeSearchItem.cs file and also comment out any Using directives.

Wow, I can't believe it took so long to figure that out. Hopefully that helps someone else out too.

GB