通过使用角后两个参数WEB API调用 [英] Pass two parameters to WEB API call using angular post
问题描述
我有以下POST方法在我的Web API控制器:
公共异步任务< HttpResponseMessage> SendPost(应用程序)
我使用称它为通过JavaScript的 angular.js 的 $ http.post
并通过应用参数作为JSON:
$ http.post(/ API / AController / SendPost,JSON.stringify(应用程序))。
成功(功能(数据,状态,头,配置){
}
这工作。
现在我想通过一个第二个参数作为一个简单的字符串(我不能修改现有的应用JSON对象)。我尝试了几种不同方法建议在网络上,但他们都不工作,我需要能够做到soemthing这样的:
控制器:
公共异步任务< HttpResponseMessage> SendPost(RentalApplication应用,串测试)
的Javascript:
$ HTTP 。员额(/ API / TessIntegration / SendPost,{应用:JSON.stringify(应用程序),测试:一些价值})
成功(功能(数据,状态,头,配置){
}
发现使用的解决方案的 Newtonsoft .Json.Linq.JObject 的:
控制器:
公共异步任务< HttpResponseMessage> 。SendPost(JObject数据)
{
RentalApplication应用=数据[应用] ToObject< RentalApplication>();
字符串测试=数据[测试] ToObject<串GT;();
}
的Javascript:
VAR数据= {
应用:应用程序,
测试:采样值
};
$ http.post(/ API / TessIntegration / SendPost数据)。
成功(功能(数据,状态,头,配置){
}
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) {
}
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) {
}
这篇关于通过使用角后两个参数WEB API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!