0

enter image description hereHi I am developing one application using web api2 and angularjs. Finding hard time to send data to web api methods. I am having problem to send data as objects n PUT and POST methods. In delete and getbyid methods i am able to send single parameter but i am not able to send data as object. I am receiving null as below. enter image description here

I am calling as below using 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. enter image description here enter image description here

May i know why i am not able to bind object to model? Any help would be appreciated. Thank you.

2
  • make sure that your sub in your JS has the same properties that the parameter NCT_UserRegistration...also try to pass data: JSON.stringify(sub) Commented Jan 24, 2017 at 13:23
  • Have u tried using headers: { 'Content-Type': 'application/json' },? Commented Jan 24, 2017 at 13:42

3 Answers 3

1
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

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

$http.post('url', person)
     .success(function (response) { 
         alert(response); 
     });
Sign up to request clarification or add additional context in comments.

2 Comments

access_token is not defined.
Hi I am able to recieve data in server. I have posted screenshot. May i know what I am missing?
1

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

Comments

0

It happens like this because you are sending an JS Object via an 'application/x-www-form-urlencoded' header. 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' }
        });
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.