ajax jQuery 3.1通过POST将数据发送到C#

时间:2017-05-15 10:23:37

标签: c# jquery ajax post

我尝试使用ajax调用方法向C#Web API发送一些数据,它可以正常使用'GET方法'到目前为止这是我的代码但是当我尝试使用'传递参数时我得到404 Not found错误POST“: 的Javascript

$.ajax({
     type: "POST",       //GET or POST or PUT or DELETE verb          
     url: "/api/account/updateuser/",       // Location of the service
     data: { Email: user, Roles: Roles },
     success: function (result) {//On Successful service call
         console.log(result);
     }
});

C#Controller:

[HttpPost]
public string UpdateUser(string Email,string Roles)
{
    return Email;
} 

我还使用PostMan谷歌浏览器扩展测试了这个Web API,它也可以正常工作,谢谢。

**编辑:当我发送没有参数的请求时,它可以正常工作。

[HttpPost]
public string UpdateUser()
{
    return "HI";
}
$.ajax({
                type: "POST",       //GET or POST or PUT or DELETE verb          
                url: "/api/account/updateuser/",       // Location of the service
                data: {},
                success: function (result) {//On Successful service call
                    console.log(result);
                }
            });

4 个答案:

答案 0 :(得分:0)

通过添加"PM_SHOWCASE"."PM_PROJECT" aggregates always(SUM of "PMBudget", SUM of "PMActual", SUM of "PMEAC"); dataType来更改您的ajax请求,以便在服务器端拥有有效的json序列化

contentType

答案 1 :(得分:0)

似乎像json序列化问题。请检查如下

var request = JSON.stringify({
         Email: user, 
         Roles: Roles
    });

$.ajax({
     type: "POST",       //GET or POST or PUT or DELETE verb          
     url: "/api/account/updateuser/",       // Location of the service
     contentType: "application/json",
     dataType: "json",
     data: request,
     success: function (result) {//On Successful service call
         console.log(result);
     }
});

在c#side

[HttpPost]
public string UpdateUser(string Email,string Roles)
{
    return Email;
} 

答案 2 :(得分:0)

尝试在控制器函数中使用ActionResult返回类型并返回内容结果对象。还要确保在将json发布到控制器之前对其进行字符串化 -

客户端脚本:

var userData = { Email: 'prashanth@gmail.com', Roles: 'Software Developer' };
            $.ajax({
                type: "POST",       //GET or POST or PUT or DELETE verb          
                url: "/api/account/updateuser/",       // Location of the service
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(userData),
                success: function (result) {//On Successful service call
                    console.log(result);
                }
            });

C#控制器代码:

[HttpPost]
public ActionResult updateUser(string Email, string Roles)
{
    return Content(Email);
}

答案 3 :(得分:0)

尝试使用对象作为参数。

您甚至可以使用动态对象参数,因此您不必为其编写类。

<强>动态

您的请求保持不变,但您应该添加数据类型:

$.ajax({
 type: "POST",       //GET or POST or PUT or DELETE verb          
 url: "/api/account/updateuser/",       // Location of the service
 data: { Email: user, Roles: Roles },
 contentType: "application/json",
 dataType: "json",
 success: function (result) {//On Successful service call
     console.log(result);
 }
});

你的C#控制器是这样的:

[HttpPost]
public string UpdateUser(dynamic User)
{
    var roles = User.Roles;
    var mail = User.Email;
    return mail;
} 

用户类

或者您为用户创建了一个类。

class User 
{ string Email; string Roles; }

并传递一个User对象:

[HttpPost]
public string UpdateUser(User User)
{
    var roles = User.Roles;
    var mail = User.Email;
    return mail;
}