Denis Wessels Denis Wessels - 3 months ago 14
AngularJS Question

Pass two parameters to WEB API call using angular post

I have the following post method in my WEB API controller:

public async Task<HttpResponseMessage> SendPost(Application application)


I call it through javascript using angular.js
$http.post
and pass through the application parameter as JSON:

$http.post("/api/AController/SendPost", JSON.stringify(application)).
success(function (data, status, headers, config) {
}


This works.

Now I want to pass through a second parameter as a simple string (I can't modify the existing application JSON object).I tried a few different ways suggested on the web but none of them seem to work. I need to be able to do soemthing like this:

Controller:

public async Task<HttpResponseMessage> SendPost(RentalApplication application,string test)


Javascript:

$http.post("/api/TessIntegration/SendPost", {application:JSON.stringify(application),test:"Some value"}).
success(function (data, status, headers, config) {
}

Answer

Found a solution using Newtonsoft.Json.Linq.JObject:

Controller:

public async Task<HttpResponseMessage> SendPost(JObject data)
{
    RentalApplication application = data["application"].ToObject<RentalApplication>();
    string test = data["test"].ToObject<string>();
}

Javascript:

        var data = {
            application : application,
            test : "sample value"
        };

        $http.post("/api/TessIntegration/SendPost",data).
        success(function (data, status, headers, config) {

}

Comments