当前位置: 移动技术网 > IT编程>开发语言>.net > 记一次webapi传参数的问题

记一次webapi传参数的问题

2018年01月15日  | 移动技术网IT编程  | 我要评论

刘艺滨,帆布鞋,天音校训通

.net小白一枚,经过了几个小时的研究,由于错误的写法导致后台始终接受不到前台传递过来的参数。首先看看控制器的参数

public Core.MVC.ServiceResult<DTO.Out.MenberLoginOutDto> Login(dynamic obj)
        {
            tb_Member temp = Service.MemberService.LoginNew(obj.Account.ToString(), obj.Pwd.ToString());
        }

本人实在是偷懒才使用dynamic关键字,ajax使用如下写法,会一直出现 不能绑定null或者是account没有之类的错误。

$.ajax({
                    type: "POST",
                    url: "http://www.lhsxpumps.com/_localhost:1566/api/logic/Login",
                    headers: {
                        "Content-Type":"application/json"
                    },
                    data: { LoginAccount: 'xxxx', LoginPassword: 'xxxxx'},
                    success: function (data, status) {

                    }
                });

设置了content-type后台也不能接收到。这比较郁闷了,因为在postman是可以访问的。但是仔细观察了请求参数后,发现了一个问题,上述方式是传递了一个对象(因为可以折叠)。postman只是传递一个json字符串,现在想想自己真的是太笨了。改成如下方式后台就可以顺利的接收到了

$.ajax({
                    type: "POST",
                    url: "http://localhost:1566/api/logic/Login",
                    headers: {
                        "Content-Type":"application/json"
                    },
                    data: JSON.stringify({ LoginAccount: 'xxxx', LoginPassword: 'xxxxx' }),
                    success: function (data, status) {

                    }
                });
View Code

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网