asp.net mvc ajax post returns 404 not found

后端 未结 1 1853
你的背包
你的背包 2021-01-23 21:11

I\'m trying to do an ajax request using jquery ajax function. I have a very strange behaviour. My folder structure ist like this:

相关标签:
1条回答
  • 2021-01-23 21:41

    Your case will work if you remove 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.

    However, we can see "Invalid JSON primitive ***" is usually the result in response.

    Here we should understand to pass the jQuery's "data" option as "{'param':'value'}", not {'param':'value'}.

    So in your case, to achieve the solution: we need to supply data as below

    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'}. (as you did)

    Hope it helps :)

    0 讨论(0)
提交回复
热议问题