无法将Json数组发布到Web核心API。

3
使用以下代码,我尝试将Json数组发布到Web Core API中的方法。然而,数组以null形式到达。
importProperties = function (url, callback, data) {
        var properties = JSON.stringify(data);
        $.ajax({
            url: url,
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: { "": properties }
        })
            .done(callback)
            .fail(errorMessage);
    }

一旦数据被JSON.stringify处理,它的形式如下:
[{"UPRN":"12345","StreetName":"COMPORT GREEN","AddressLine2":"NEW ADDINGTON","AddressLine3":"Croydon","AddressLine4":"Surrey","Postcode":"CR0 0BY","Latitude":null,"Longitude":null,"BlockUPRN":"B12345","PropertyNo":"1","BlockName":"Block Court","Comments":null,"PropertyTypeId":6,"NumberOfBathrooms":1,"NumberOfBedrooms":2,"NumberOfKitchens":1,"PropertyContractId":0,"PropertyType":null},
{"UPRN":"67890","StreetName":"COMPORT GREEN","AddressLine2":"NEW ADDINGTON","AddressLine3":"Croydon","AddressLine4":"Surrey","Postcode":"CR0 0BY","Latitude":null,"Longitude":null,"BlockUPRN":"B67890","PropertyNo":"2","BlockName":"Block Court","Comments":null,"PropertyTypeId":6,"NumberOfBathrooms":null,"NumberOfBedrooms":null,"NumberOfKitchens":null,"PropertyContractId":0,"PropertyType":null}]

我的Web核心API的方法签名是:

[HttpPost("{contractId}/import")]
public async Task<IActionResult> Import(int contractId, [FromBody] IEnumerable<PropertyAndContractDto> properties)
{
   this.NLogger.Info($"api/property/contractId = {contractId}/saveproperties".ToPrefix());
   if (properties == null)
   {
      return BadRequest();
   }
...
}

DTO是数据传输对象
    public class PropertyAndContractDto
    {
        public string UPRN { get; set; }
        public string StreetName { get; set; }
        public string AddressLine2 { get; set; }
        public string AddressLine3 { get; set; }
        public string AddressLine4 { get; set; }
        public string Postcode { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string BlockUPRN { get; set; }
        public string PropertyNo { get; set; }
        public string BlockName { get; set; }
        public string Comments { get; set; }
        public int? PropertyTypeId { get; set; }
        public int? NumberOfBathrooms { get; set; }
        public int? NumberOfBedrooms { get; set; }
        public int? NumberOfKitchens { get; set; }
        public int PropertyContractId { get; set; }
        public string PropertyType { get; set; }
    }
}

当我在 Postman 中运行时,它可以正常工作; enter image description here 那么为什么 properties 参数会变成 null 呢?

很可能是因为它无法解析为 PropertyAndContractDto。请发布 DTO 类。类属性与字符串中的属性相同吗?首先,您能将此字符串解析为 DTO 数组吗? - Panagiotis Kanavos
我已经发布了Dto。它是Web核心API和数据的相同类。 - arame3333
你尝试过将 data: { "": properties } 更改为 data: { "properties": properties } 吗? - Siva Gopal
@Sebastian - 我尝试了,但结果还是一样。 - arame3333
@Siva - 是的,但没有区别。那是我最初尝试的。 - arame3333
显示剩余2条评论
1个回答

2
尝试将ajax调用的processData属性设置为false,如下所示:
$.ajax({
            url: url,
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: { "": properties },
            processData: false
        })

根据文档
默认情况下,传递给数据选项的数据对象(技术上,除字符串外的任何内容)将被处理并转换为查询字符串,适合默认内容类型“application/x-www-form-urlencoded”。 如果要发送DOMDocument或其他未经处理的数据,请将此选项设置为false。
或者,只需将data属性设置为您的字符串化json即可:
$.ajax({
            url: url,
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: properties
        })

你的第二个建议就是答案。我以为我尝试了每一个替代方案,但在进行了许多更正之后,我没有回到这个方案。所以谢谢你。 - arame3333

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接