开发者

Jquery .Ajax error when trying to POST data in ASP.NET MVC

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.


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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜