简体   繁体   English

无法使用angularjs将数据发送到Web API方法

[英]Unable to send data to web api methods using angularjs

在此处输入图片说明 Hi I am developing one application using web api2 and angularjs. 嗨,我正在使用Web api2和angularjs开发一个应用程序。 Finding hard time to send data to web api methods. 很难将数据发送到Web api方法。 I am having problem to send data as objects n PUT and POST methods. 我在将数据作为对象发送到PUT和POST方法时遇到问题。 In delete and getbyid methods i am able to send single parameter but i am not able to send data as object. 在delete和getbyid方法中,我可以发送单个参数,但不能将数据作为对象发送。 I am receiving null as below. 我收到空值,如下所示。 在此处输入图片说明

I am calling as below using angularjs. 我正在使用angularjs如下调用。

this.saveSubscriber = function (sub) {
        return $http({
            method: 'post',
            data: sub,
            url: '/NCT_Users/',
          // contentType: "application/json"
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        });
    }

If i comment header and uncomment contentType in above code I am getting totally null object as below. 如果我在上面的代码中注释标题和取消注释contentType,我将得到完全为null的对象,如下所示。 在此处输入图片说明 在此处输入图片说明

May i know why i am not able to bind object to model? 我可以知道为什么我无法将对象绑定到模型吗? Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

var person = {firstName:"John", lastName:"Doe", age:46};

$http.post('url', person)
       .success(function (response){
            alert(response);
           });

accesstoken is defined variable.you can define your variables to pass it to server accesstoken是定义的变量。您可以定义变量以将其传递给服务器

var person = {
               firstName:"John", 
               lastName:"Doe", 
               age:46
             };

$http.post('url', person)
     .success(function (response) { 
         alert(response); 
     });

try this way. 尝试这种方式。

var sub = {
             User_CreatedDate: "", 
             UserEmailId: "", 
             User_Id: "", 
             User_MobileNum: "", 
             User_Name: "", 
             User_Password: "", 
             User_Role: "", 
             User_Status: "", 
             User_UpdateDate: ""
          };

    $http.post('/NCT_Users/', sub).success(function (response) { alert(response); });

the fields are filled by you 字段由您填充

It happens like this because you are sending an JS Object via an ' application/x-www-form-urlencoded ' header. 发生这种情况是因为您是通过' application / x-www-form-urlencoded '标头发送JS对象的。 You have to send the data as parameters, try this updated function: 您必须将数据作为参数发送,请尝试以下更新的功能:

this.saveSubscriber = function (sub) {
        return $http({
            method: 'POST',
            data: $.param(sub),
            url: '/NCT_Users/',
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        });
}

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

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