简体   繁体   English

asp.net mvc ajax发布返回404找不到

[英]asp.net mvc ajax post returns 404 not found

I'm trying to do an ajax request using jquery ajax function. 我正在尝试使用jquery ajax函数执行ajax请求。 I have a very strange behaviour. 我的行为很奇怪。 My folder structure ist like this: 我的文件夹结构如下:

文件夹结构

My client-side ajax call (in UserManagement/Index.cshtml) looks like the following: 我的客户端ajax调用(在UserManagement / Index.cshtml中)如下所示:

var rolename = viewModel.roleName();
$.ajax({
     type: "POST",
     url: '@Url.Action("AddRole", "UserManagement")',
     contentType: "application/json; charset=utf-8",
     data: { rolename: rolename },
     success: function(data) {
         var role = new Role(rolename);
         model.addRole(role);
         model.appendItem("#accordion", role.getItem());
         viewModel.roleName("");            
     }
});

In my UserManagementController.cs I have the following actions: 在我的UserManagementController.cs中,我执行以下操作:

    // GET: /UserManagement/
    public ActionResult Index()
    {
        var serializer = new JavaScriptSerializer();
        var context = new ApplicationDbContext();
        var users = context.Users.ToList();
        var roles = context.Roles.ToList();


        ViewBag.Users = users;
        ViewBag.Roles = roles;
        return View();
    }

    //
    // POST: /UserManagement/AddRole
    [HttpPost]
    public string AddRole(string rolename)
    {

        Response.StatusCode = 200;
        return "gsfgdg";
    }

I already tried to use a GET instead of POST or to change AddRole to void type and anything other but I can't get this to work. 我已经尝试使用GET而不是POST或将AddRole更改为void类型以及其他任何内容,但是我无法使其正常工作。 I always receive a "404 not found". 我总是收到“找不到404”。 Can anybody help? 有人可以帮忙吗?

Thanks in advance! 提前致谢!

Your case will work if you remove Content-Type. 如果删除Content-Type,您的案例将起作用。

Case 1: If we use Content-Type: "application/json"

When you request with a Content-Type of application/json, in server side ScriptServices expects data as JSON serialized string. 当您请求Content / Type的application / json时,在服务器端ScriptServices期望数据为JSON序列化字符串。

However, we can see "Invalid JSON primitive ***" is usually the result in response. 但是,我们可以看到"Invalid JSON primitive ***"通常是响应的结果。

Here we should understand to pass the jQuery's "data" option as "{'param':'value'}", not {'param':'value'}. 在这里,我们应该理解将jQuery的“数据”选项传递为“ {'param':'value'}”,而不是{'param':'value'}。

So in your case, to achieve the solution: we need to supply data as below 因此,就您而言,要实现解决方案:我们需要提供以下data

var rolename = viewModel.roleName();
$.ajax({
     type: "POST",
     url: '@Url.Action("AddRole", "UserManagement")',
     contentType: "application/json; charset=utf-8",
     data: "{ 'rolename':'" + rolename +"' }",
     success: function(data) {
         var role = new Role(rolename);
         model.addRole(role);
         model.appendItem("#accordion", role.getItem());
         viewModel.roleName("");            
     }
});

Case 2: If we skip Content-Type: "application/json"

We should supply jQuery's "data" option as JSON object {'param':'value'} or {param:'value'}. 我们应该提供jQuery的“数据”选项作为JSON对象{'param':'value'}或{param:'value'}。 (as you did) (与您一样)

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM