0

I have a ASP.Net web API with one post method. I'm calling the post method from angular js. It is not passing me the data to API POST method, All my properties of my requestData object is null. I'm not sure what is the mistake am doing here. Can anyone help me plz.

API Code

public void Post(RequestData data)
{
.....
}

public class RequestData
{
    PropertyDetail propertyDetails;
    ICollection<Model1> model1s;
    ICollection<Model2> model2s;
    ICollection<Model3> model3;
    ICollection<Model4> model4;
}

Client Code

var requesData = new RequestData();
requesData.model0= $scope.model0;
requesData.model1s= $scope.models;
requesData.model2s= $scope.model2s;
requesData.model3s= $scope.model3s;
requesData.model4s= $scope.model4s;

        $http({
            method: 'POST',
            url: window.apiUrl,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: requesData,
        }).then(function (res) {
            console.log('succes !', res.data);
            window.alert("Successfully created");
        }).catch(function (err) {
            debugger;
            console.log('error...', err);
        });
2
  • make sure the model you receive from js file, is in sync with names of each property on model in .cs file (which are different as of now) Commented Mar 26, 2018 at 11:49
  • Actually i changed my actual property name with dummy names here model0,model1, etc but the names are correct in code. Commented Mar 26, 2018 at 12:03

3 Answers 3

1

Probably, your server side couldn't map your parameters correctly. Data type matching is important while post some parameters. You can change your client code like this:

...
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(requestData),
        dataType:'json',
...
Sign up to request clarification or add additional context in comments.

1 Comment

Actually i noticed that below line is causing to post the entire html page. headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, If i remove this header it throws me an another error "Accessing the 'arguments' property of a function is not allowed in strict mode".
0

After doing as @Jaky71 says, you can learn how .net expects those objects by calling dummy methods with nulls or whatever you nees to mimic.

.net has a strict parser

2 Comments

Actually i noticed that below line is causing to post the entire html page. headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, If i remove this header it throws me an another error "Accessing the 'arguments' property of a function is not allowed in strict mode"
You shouldn't remove just change it to application/json
0

You can use angular.toJson:

$http({
        method: 'POST',
        url: window.apiUrl,
        headers: { 'Content-Type': 'application/json' },
        data: angular.toJson(requesData),
    }).then(function (res) {
        console.log('succes !', res.data);
        window.alert("Successfully created");
    }).catch(function (err) {
        debugger;
        console.log('error...', err);
    });  

Also make sure that your properties match.

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.