无法将Json数组发布到Web核心API(Cannot post Json array to web core api)
使用此代码,我试图将Json数组发布到我的Web核心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.stringified,它看起来像;
[{"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; } } }
当我在邮差中跑步时,它有效;
那么为什么属性参数以null形式出现?
With this code I am trying to post a Json array to a method in my web core API. However the array arrives as 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); }
Once the data is JSON.stringified, it looks like;
[{"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}]
The method signature on my web core API is;
[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(); } ... }
The DTO is
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; } } }
When I run in Postman it works;
So why is the properties parameter coming in as null?
原文:https://stackoverflow.com/questions/45728774
最满意答案
使用属性以selector开头 :
$('[id^="myTable"]').on('change', function(){ var $id = this.id; // do stuff });
这将针对ID以myTable开头的所有元素为目标,例如
myTable1
,myTable2
,myTableStackOverflowChineseOldMan
等。使用PHP插入元素并不能使它们动态化,因此不需要委托事件处理程序。
Use the attribute starts with selector :
$('[id^="myTable"]').on('change', function(){ var $id = this.id; // do stuff });
That will target all elements with an ID starting with myTable, for instance
myTable1
,myTable2
,myTableStackOverflowChineseOldMan
etc.Inserting the elements with PHP doesn't really make them dynamic, so delegated event handlers shouldn't be needed for this.
相关问答
更多-
添加点击处理程序时,可能尚未在浏览器上更新DOM。 你有几个选择: 使用委托绑定(使用.on) 在将事件添加到DOM之前附加事件 例1: $('body').on('click', '#' +id +'button', function () { /* foo */ }); 例2: $('#' +id +'button', myHtml).on('click', function () { /* foo */ }); List.append(myHtml); The DOM might not have ...
-
事实证明,这实际上是正常工作,我已经误解了规范 当在与本地存储区域相关联的Storage对象x上调用setItem(),removeItem()和clear()方法时,如果方法执行某些操作,则在Window对象的localStorage属性的Storage对象的每个Document对象中与x相同的存储区域相关联,必须触发存储事件 换句话说,在更新localStorage对象并导致事件的情况下,每个窗口/选项卡都会触发存储事件。 所以事件没有被解雇,因为我只有一个窗口/标签打开。 如果我将上述代码放在一个页面 ...
-
jquery:当输入不是来自用户时,如何绑定更改事件(jquery : How to bind change event when the input is not from the user)[2023-06-15]
以编程方式更改输入的值不会触发change事件。 你必须自己触发事件。 由于您使用的是jQuery,因此可以使用它的.trigger()或.change()方法。 $('input[name="DateFrom"]').val(newVal).trigger('change'); Changing value of an input programmatically doesn't trigger change event. You have to trigger the event yourself. ... -
使用属性以selector开头 : $('[id^="myTable"]').on('change', function(){ var $id = this.id; // do stuff }); 这将针对ID以myTable开头的所有元素为目标,例如myTable1 , myTable2 , myTableStackOverflowChineseOldMan等。 使用PHP插入元素并不能使它们动态化,因此不需要委托事件处理程序。 Use the attribute starts with ...
-
1)您忘记使用'settings'前缀: BindEvents: function (options) { // defaults do not make sense this way, but they probably have a sane default in your code, right? var defaults = { Control: null, Events: null } settings = $.extend({}, ...
-
您需要使用事件委派 。 您必须使用.on()使用委托事件方法。 即 $(document).on('event','selector',callback_function) 在你的情况 $(document).on('click', '.myclass > li', function () { ... }); 或者如果您想申请所有列表项目: $(".myclass").on('click', '> li', function () { ... }); You need to use E ...
-
Jquery Mobile动态创建的范围更改事件未触发(Jquery Mobile dynamic created range change event not firing)[2022-02-02]
您需要做的两件事1.将css类brightness添加到您的范围输入 $('').appendTo("fieldset"); 使用on添加事件处理程序 $(document).on("change",".brightness",function() { al ... -
可以循环播放它们: $(jqObjs).each(function(_, jQobj){ jQobj.on("blur change",function () { callEventHandler(this); }); }); 由于bind() on()内部使用on()我改用on()代替。 另一种方法是只存储选择器 var jqSelectors = ['#name', '#domain', '#description']; $(jqObjs.join()).on('.. ...
-
如果您真的想要使用它,可以使用属性选择器 类似[attr*=value] ,其中attr包含字符串value 。 例如(在jQuery中): var $btnElement = $('[class*="clickme"]'); 以上将选择具有包含clickme的类属性的任何元素 If this is really how you want to use it, you could use an attribute selector Something like [attr*=value] where th ...
-
你需要使用这样的事件删除 - $(document.body).on('change','input[type="checkbox"]', function () { You need to use event delegation like this - $(document.body).on('change','input[type="checkbox"]', function () {