我们正试图从客户端调用web-api。 web api包含一个Request类,其Amount为十进制。我们试图传递一个小数,并从webapi和状态返回相同的数量。
问题是 web-api无法收到金额。当我们尝试使用webapi附加时,我们在Request类的Amount字段中只看到null
。
在这方面的任何帮助都会很棒。
以下是我们从客户端打电话的方式
var requestMessage = { RequestSale: { Amount: '100.545M' } };
var request = {
type: "POST",
contentType: "application/json;charset=utf-8",
datatype: "json",
url: "http://<machinename>/Demo/api/monthlySales/InsertMonthlySales",
successCallback: successCallback,
errorCallback: errorCallback,
filterCriteria: null,
data: JSON.stringify(requestMessage)
//header: { key: "Accept", value: "application/json" },
};
utilityTest.getData(request);
expect(true, true);
utilityTest.getData(request) - 这使得实际调用,下面是代码
utilityTest.getData = function (request) {
$.ajax({
type: request.type,
contentType: request.contentType,
datatype: request.datatype,
url: request.url,
data: request.data,
cache : false,
beforeSend: function (xmlHttpRequest) {
if (request.header) {
xmlHttpRequest.setRequestHeader(request.header.key, request.header.value);
}
},
success: function (data, textStatus, xmlHttpRequest) {
if (request.successCallback) {
request.successCallback(data, request.filterCriteria);
}
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
if (request.errorCallback)
request.errorCallback(xmlHttpRequest, textStatus, errorThrown);
} });
Web-api方法
[HttpPost]
public ResponseSale InsertMonthlySales(RequestSale sale)
{
var curMonth = DateTime.UtcNow.Month;
var salesAmount = Convert.ToDecimal(sale.Amount);
ResponseSale response = new ResponseSale();
response.Amount = salesAmount;
response.Id = curMonth;
if (monthlySales.ContainsKey(curMonth))
{
monthlySales[curMonth] += salesAmount;
}
else
{
monthlySales.Add(curMonth, salesAmount);
}
response.Status = "InsertMonthlySales";
return response;
web-api的请求/响应合同
namespace DemoContracts.Object
{
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://DemoSchema.ContractSale")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://DemoSchema.ContractSale", IsNullable = false)]
public class RequestSale {
public decimal Amount { get; set; }
}
}
namespace DemoContracts.Object
{
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://DemoSchema.ContractSale")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://DemoSchema.ContractSale", IsNullable = false)]
public class ResponseSale
{
public string Status { get; set; }
public decimal Amount { get; set; }
public int Id { get; set; }
}
- 从IE中捕获的JSON 请求机构: {&#34; RequestSale&#34; {&#34;量&#34;:100.545}}
回应机构 {&#34;状态&#34;:&#34; InsertMonthlySales&#34;&#34;量&#34;:0.0&#34;标识&#34;:9}
在Web API中,InsertMonthSales请求对象RequestSale.Amount始终为0.我们没有获得传递的100.545值