将已发布的JSON数据发布到Web API的问题 [英] Issue with posted JSON data to web API
问题描述
我有一个网页,可以将数据发布到Web API。
ajax函数如下。
Hi,
I have a web page which posts data to an Web API.
The ajax function is as follows.
var jsondata = { "comparisionCalculatorReq": { "transactionType": "None", "fundName": "Myfund", "annualizedReturn": "10", "frequency": "Monthly"} };
$.ajax({
type: "POST",
url: "http://localhost:1101/Mypost/PostDataAPI",
dataType: 'json',
data: jsondata
}).done(function (msg) {
alert(msg);
});
在我的Web API控制器中,我有一个如下方法
In my Web API controller i have a method as below
[ActionName("PostDataAPI")]
public string PostDataAPI(JSONdata obj)
{
try
{
string k = obj.transactionType;
return k;
}
catch (Exception err)
{
throw err;
}
}
JSONdata类位于以下
The JSONdata class is below
public class JSONdata
{
public string transactionType { get; set; }
public string fundName { get; set; }
public string annualizedReturn { get; set; }
public string frequency { get; set; }
}
问题是,我得到的是transactionType的空值我的控制器。
如果json数据在ajax中这样传递,
Problem is that, I'm getting null value for transactionType in my controller.
If the json data is passed like this in ajax,
var jsondata={ "transactionType": "None", "fundName": "Myfund", "annualizedReturn": "10", "frequency": "Monthly"};
然后所有的值都是正确的。
请为此提供解决方案。
我尝试过:
我试过制作如下的JSONdata类,但是它仍然为每个值返回null
then all the values are got correct.
Please provide a solution for this.
What I have tried:
I have tried making the JSONdata class as below, but still it is returning null for every values
public class JSONdata
{
public object comparisionCalculatorReq { get; set; }
public string transactionType { get; set; }
public string fundName { get; set; }
public string annualizedReturn { get; set; }
public string frequency { get; set; }
}
推荐答案
.ajax({
type: POST,
url: http:// localhost:1101 / Mypost / PostDataAPI,
dataType:' json',
数据:jsondata
})。done( function (msg){
alert(msg) );
});
.ajax({ type: "POST", url: "http://localhost:1101/Mypost/PostDataAPI", dataType: 'json', data: jsondata }).done(function (msg) { alert(msg); });
在我的Web API控制器中,我有一个如下方法
In my Web API controller i have a method as below
[ActionName("PostDataAPI")]
public string PostDataAPI(JSONdata obj)
{
try
{
string k = obj.transactionType;
return k;
}
catch (Exception err)
{
throw err;
}
}
JSONdata类位于以下
The JSONdata class is below
public class JSONdata
{
public string transactionType { get; set; }
public string fundName { get; set; }
public string annualizedReturn { get; set; }
public string frequency { get; set; }
}
问题是,我得到的是transactionType的空值我的控制器。
如果json数据在ajax中这样传递,
Problem is that, I'm getting null value for transactionType in my controller.
If the json data is passed like this in ajax,
var jsondata={ "transactionType": "None", "fundName": "Myfund", "annualizedReturn": "10", "frequency": "Monthly"};
然后所有的值都是正确的。
请为此提供解决方案。
我尝试过:
我试过制作如下的JSONdata类,但是它仍然为每个值返回null
then all the values are got correct.
Please provide a solution for this.
What I have tried:
I have tried making the JSONdata class as below, but still it is returning null for every values
public class JSONdata
{
public object comparisionCalculatorReq { get; set; }
public string transactionType { get; set; }
public string fundName { get; set; }
public string annualizedReturn { get; set; }
public string frequency { get; set; }
}
我修改了你的ajax代码:
I modified your ajax code:
var jsondata = { "comparisionCalculatorReq": { "transactionType": "None", "fundName": "Myfund", "annualizedReturn": "10", "frequency": "Monthly"} };
.ajax({
type: < span class =c ode-string> POST,
url: http:// localhost:1101 / Mypost / PostDataAPI,
contentType: application / json; charset = utf-8,
dataType:' json',
数据: JSON .stringify(jsondata)
})。done( function ( msg){
alert(msg);
});
.ajax({ type: "POST", url: "http://localhost:1101/Mypost/PostDataAPI", contentType:"application/json; charset=utf-8", dataType: 'json', data: JSON.stringify(jsondata) }).done(function (msg) { alert(msg); });
这篇关于将已发布的JSON数据发布到Web API的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!