为什么我无法接收通过ajax呼叫发送的数据(发布请求)? [英] Why i am unable to receive the data sent through ajax call(post request) ?

查看:78
本文介绍了为什么我无法接收通过ajax呼叫发送的数据(发布请求)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过客户端代码(jquery ajax调用)中的POSTRequest插入数据。

当请求转到控制器方法时,我无法检索发送的数据。当它到达网络时api控制器数据为空。



我尝试过:



客户代码:



I am trying to insert data through POSTRequest in clientside code(jquery ajax call).
When the request goes to controller methods i am not able to retrieve the data sent.When it hits the web api controlller data is null.

What I have tried:

Clientside code:

function AddUser()
        {         
            var user = { UserID: $("#UidTxt").val, UserName: $("#UnameTxt").val, Password: $("#PwdTxt").val, Email: $("#EmailTxt").val, Address: $("#AddrTxt").val, DOB: $("#DobTxt").val, Phone: $("#PhoneTxt") };
            $.ajax({
                url: 'http://localhost:61540/api/UserDetails/AddUser',
                type: POST,
                data: JSON.stringify(user),
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    alert("success! data is posted/inserted.")
                },
                error: function (xhr, status) {
                    alert(xhr.ResponseText);
                }
            });
        }



Web Api控制器方法:




Web Api Controller Methods:

[System.Web.Http.AcceptVerbs("POST")]
[ActionName("AddUser")]
[System.Web.Http.HttpPost]
[System.Web.Http.HttpOptions]
public List<UserDetails> AddUser(UserDetail oUserList)
{
    if (oUserList != null)
    {
    db.UserDetails.Add(oUserList);
    db.SaveChanges();
    }
    var userList = db.UserDetails.ToList();
    return DTOConverter.ConvertUserDetail(userList);
}





调试时,调用命中控制器方法,我发现oUserList对象为空。

我正确传递数据吗?如果我错了,请纠正我。

提前谢谢。



While Debugging,the call hits the controller methods and i find oUserList object is null.
Am i passing the data correctly? correct me if am wrong.
Thanks in advance .

推荐答案

#UidTxt)。val,UserName:
("#UidTxt").val, UserName:


#UnameTxt)。val,密码:
("#UnameTxt").val, Password:


#PwdTxt)。val,电子邮件:
("#PwdTxt").val, Email:


这篇关于为什么我无法接收通过ajax呼叫发送的数据(发布请求)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆