Soner
Soner

Reputation: 1282

Angular JS Post is not working

I am using angularjs and ionic.I post multiple data from angular js to Api Controller in asp.net mvc

I have below code in order to post multiple form data to controller in asp.net mvc from angularjs.

  $http({
        method: 'POST',
        url: 'http://localhost:51425/api/values/Response',
        params: { PropertyFirst: "1", PropertySecond: "2" },
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    }).success(function (data) {
        alert("ok");
    });

If i use below code this works

[HttpPost]
public object Response(string PropertyFirst , string PropertySecond)
{

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");


return null;
}

However, if i use below code it is not working

PropertyClass

public string PropertyFirst{ get; set; }
public string PropertySecond{ get; set; }

Controller side

[HttpPost]
public object Response(PropertyClass propertyValues)
{

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

return null;
}

I get below error :

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Question:

How can i post PropertyFirst and PropertySecond to PropertyClass ?

Any help will be appreciated.

Thanks.

Upvotes: 1

Views: 104

Answers (1)

awimley
awimley

Reputation: 712

From the Angular $HTTP docs :

params – {Object.} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

I don't know much about asp MVC framework, but if you change the 'params' key to a 'data' key, your request will be correct.

Unfortunately I can't really help with the ASP controllers, as I am entirely unfamiliar with that language.

Upvotes: 1

Related Questions