使用jQuery发布时,收到拼写错误的JSON模型(When post with jQuery, received JSON model with incorrect spelling)

编程入门 行业动态 更新时间:2024-10-15 14:12:56
使用jQuery发布时,收到拼写错误的JSON模型(When post with jQuery, received JSON model with incorrect spelling)

我正在使用ASP.NET Core,现在尝试使用post与jQuery。 我遇到的情况是这样的:

我有一个模特

public class Model { int INT_VALUE { get; set; } string STR_VALUE { get; set; } int SOME { get; set; } }

和API一样

[HttpPost] public Model GetValues(string val) { Model m = new Model { INT_VALUE = 1, STR_VALUE = "hi", SOME = "there" }; return m; }

现在我使用jQuery请求post

$.post('api/GetValues', { val: "abc" }) .done(function (data) { console.log(data) if (data.INT_VALUE === 1) { console.log("Hit!"); } });

关键是如果我得到INT_VALUE的模型=== 1 log'Hit !' 但它不起作用。 (没有进入console.log("Hit!") )

所以,我检查接收的json值,然后我发现了拼写错误的奇怪值

{"inT_VALUE":1,"stR_VALUE":"hi","some":"there"}

我的期望是什么

{"INT_VALUE":1,"STR_VALUE":"hi","SOME":"there"}

要么

{"int_value":1,"str_value":"hi","some":"there"}

有没有办法改变我能期待的拼写?

I'm using ASP.NET Core and now trying to use post with jQuery. The situation I encounter was like this:

I have a model

public class Model { int INT_VALUE { get; set; } string STR_VALUE { get; set; } int SOME { get; set; } }

and an API like

[HttpPost] public Model GetValues(string val) { Model m = new Model { INT_VALUE = 1, STR_VALUE = "hi", SOME = "there" }; return m; }

Now I request with post using jQuery

$.post('api/GetValues', { val: "abc" }) .done(function (data) { console.log(data) if (data.INT_VALUE === 1) { console.log("Hit!"); } });

The point is that if I get Model with INT_VALUE === 1 log 'Hit!' but it did not work. (did not get in console.log("Hit!"))

So, I check the receiving json value then I found strange values with wrong spelling like

{"inT_VALUE":1,"stR_VALUE":"hi","some":"there"}

What I expected is

{"INT_VALUE":1,"STR_VALUE":"hi","SOME":"there"}

or

{"int_value":1,"str_value":"hi","some":"there"}

Is there any way to change spelling which I can expect?

最满意答案

我认为您可以更改为使用PascalCase来获取模型的属性(例如:IntValue,StrValue ...)。 然后你可以通过以下设置强制Json Serializer使用PascalCase。

在Startup.cs文件中,找到您的ConfigureServices方法并像这样更新它

public void ConfigureServices(IServiceCollection services) { services.AddMvc() .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); }

然后在客户端,您可以使用模型中定义的相同名称访问属性(IntValue,StrValue ...)。

I think you could change to use PascalCase for properties of your model (ex: IntValue, StrValue...). Then you could force Json Serializer to use PascalCase by following setting.

In your Startup.cs file, find your ConfigureServices method and update it like this

public void ConfigureServices(IServiceCollection services) { services.AddMvc() .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); }

Then in client side, you could access property using the same name as defined in your model (IntValue, StrValue...).

更多推荐

本文发布于:2023-08-06 18:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1452781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   错误   JSON   jQuery   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!