首页 \ 问答 \ 传递给WEb API Action时,在JSON中丢失值(Losing values in JSON when passing to WEb API Action)

传递给WEb API Action时,在JSON中丢失值(Losing values in JSON when passing to WEb API Action)

我有一个具有一些属性的对象(A),其中一个属性是一个较低类型的列表(B)。

我有一个Web API Action,它接受一个A对象作为参数。

我只是测试我可以通过JSON儿子传递这个对象我有一个带有一些Javascript的webform,如下所示.......

    var tmpData = {
        lid: "f8fdb980-ccb8-4a54-9b83-b73dd2d569ca",
        aid: "8f6efc68-d747-42a4-b7d4-218951b66a97",
        bid: "e9f5e5d2-5d3d-41ac-89dc-7586ec2a5286",
        ps: []
    };
    tmpData['ps'].push({ "cid": "5a664dcc-8281-41f1-b81c-ae49499e12b8", "d": 5, "q": 2 });
    tmpData['ps'].push({ "cid": "4e9a30e0-c741-4708-88d7-8db4941c17cc", "d": 10, "q": 2 });

    var myJSON = JSON.stringify(tmpData);

    //Call the action to get the list in JSON format
    url = "http://localhost:64878/home/TakeTestBasketAddItemsRequest";
    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        data: myJSON,
        success: function (resultantData) {
            var s = resultantData;
            pResults.innerHTML = s;
        }
    });

这将导致操作OK,并且对象在“ps”属性中有2个项目。 然而,这两个项目中的值丢失了,我留下了零/未初始化的值。

我是否丢失了列表项的值?

为清楚起见 - 这里也是我的行动。

    [HttpPost]
    public ActionResult TakeTestBasketAddItemsRequest(ECodeBasketRequest model)
    {
        try
        {
            var sb = new StringBuilder();
            sb.AppendLine(String.Format("LocationBridgeId: {0}{1}", model.lid, Environment.NewLine));
            sb.AppendLine(String.Format("ApplicationBridgeId: {0}{1}", model.aid, Environment.NewLine));
            sb.AppendLine(String.Format("BasketId: {0}{1}", model.bid, Environment.NewLine));
            foreach (var p in model.ps)
            {
                sb.AppendLine(String.Format("CategoryBridgeId: {2} Denomination: {0} Quantity: {1}{3}", p.d, p.q, p.cid, Environment.NewLine));
            }
            return Content(sb.ToString());
        }
        catch (Exception ex)
        {
            return Content(string.Format("Problem: {0}", ex.ToString()));
        }
    }

正如我所说的那样,我现在只是在测试,所以我希望能够深入了解这一点。 感谢先生,Ant


I have an object (A) with a few properties, one of which is a LIST of abother type (B).

I have a Web API Action that takes in an A object as a parameter.

I am just testing that I can pass this object via JSON son I have a webform with some Javascript on it as follows.......

    var tmpData = {
        lid: "f8fdb980-ccb8-4a54-9b83-b73dd2d569ca",
        aid: "8f6efc68-d747-42a4-b7d4-218951b66a97",
        bid: "e9f5e5d2-5d3d-41ac-89dc-7586ec2a5286",
        ps: []
    };
    tmpData['ps'].push({ "cid": "5a664dcc-8281-41f1-b81c-ae49499e12b8", "d": 5, "q": 2 });
    tmpData['ps'].push({ "cid": "4e9a30e0-c741-4708-88d7-8db4941c17cc", "d": 10, "q": 2 });

    var myJSON = JSON.stringify(tmpData);

    //Call the action to get the list in JSON format
    url = "http://localhost:64878/home/TakeTestBasketAddItemsRequest";
    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        data: myJSON,
        success: function (resultantData) {
            var s = resultantData;
            pResults.innerHTML = s;
        }
    });

This gets to the action OK and the object has 2 items in the "ps" property. Howeverm the values in these 2 items are lost and I am left with zeros / uninitialised values.

Whay am I losing the values of the list items?

For clarity - here is my Action also.

    [HttpPost]
    public ActionResult TakeTestBasketAddItemsRequest(ECodeBasketRequest model)
    {
        try
        {
            var sb = new StringBuilder();
            sb.AppendLine(String.Format("LocationBridgeId: {0}{1}", model.lid, Environment.NewLine));
            sb.AppendLine(String.Format("ApplicationBridgeId: {0}{1}", model.aid, Environment.NewLine));
            sb.AppendLine(String.Format("BasketId: {0}{1}", model.bid, Environment.NewLine));
            foreach (var p in model.ps)
            {
                sb.AppendLine(String.Format("CategoryBridgeId: {2} Denomination: {0} Quantity: {1}{3}", p.d, p.q, p.cid, Environment.NewLine));
            }
            return Content(sb.ToString());
        }
        catch (Exception ex)
        {
            return Content(string.Format("Problem: {0}", ex.ToString()));
        }
    }

As I said, I am just testing at the moment so would love to get to the bottom of this. Thanks in advace, Ant


原文:https://stackoverflow.com/questions/33168199
更新时间:2023-07-31 07:07

最满意答案

安全始终是一种方便的权衡。 没有真正的绝对。 对于访问存储库和可能访问密钥的(潜在)git主机的任何人,您是否很酷? 所有开发人员的计算机都安全且使用磁盘加密吗? 密钥可以访问什么? AWS上的一切? 您是否相信能够访问回购的人足以保持安全并且不会意外地分享进一步的东西? 关键是你需要尝试完全理解秘密可能泄露的方式。

就个人而言,我宁愿将它们置于git之外,并通过usb棒或加密电子邮件将密钥分发给开发人员。 而且只针对需要它们的人。 只有我们开发人员的一部分实际上需要访问。

如果您确定风险过大,请确保更换所有秘密。 不要只是从存储库中删除它们。 一旦他们在那里,他们永远在那里。


Security is always a trade-off with convenience. There are no real absolutes. Are you cool with anyone who accesses the repository and the (potential) git host having access to the keys? Are all developers' computer secure and use disk encryption? What do the keys give access to? Everything on AWS? Do you trust people with access to the repo enough to stay secure and not accidentally share things further? The point is that you need to try to fully understand in which ways the secrets may be leaked.

Personally I rather keep them outside of git and distribute keys to developers via usb sticks or encrypted email. And only to the people who need them. Only a subset of our developers actually need access.

Just make sure that you replace all your secrets if you do decide it's too risky. Don't just delete them from the repository. Once they are in there, they are in there forever.

相关问答

更多
  • 你需要使用签名的请求。 基本上它的工作原理是这样的: 你给你的用户一个API密钥和一个只有你和客户知道的“秘密”(一个随机字符串)。 每当他们提出请求时,他们都会添加一个“签名”参数。 该签名基本上是请求参数+ API密钥+其他参数(见下文)+秘密的散列。 既然你也知道这个秘密,你可以验证签名是否正确。 为了避免重放攻击,您还可以将混合和时间戳添加到混合中。 一个随机数只是一个数字,必须由客户端在每个请求中增加。 当你收到请求时,你会检查你是否已经收到了这个nonce / timestamp。 如果你这样做 ...
  • 基本上阐述了这里概述的内容。 以下是它的工作原理:假设我们有一个从零到九的数字的函数,增加三个,如果结果大于十,则减去十个。 所以f(2)= 5,f(8)= 1等。现在,我们可以通过添加七个而不是三个来创建另一个函数,称为f'。 f'(5)= 2,f'(1)= 8等 这是一个双向函数及其逆的例子。 理论上,将一件事映射到另一件事物的任何数学函数都可以颠倒。 但实际上,您可以制作一个功能,使其输入更加顺畅,从而难以扭转。 输入和应用单向函数称为“散列”输入,而Amazon在其系统上存储的是密钥的“哈希”。 S ...
  • 假设此脚本以交互方式运行,您可以要求用户通过调用input()或raw_input() (取决于您正在使用的Python版本raw_input()在键盘上键入密码。 您可以将密码存储在单独的文件中,这不是完全安全的,但可以让您在不共享密码的情况下共享脚本。 您可以获取用户名+密码的base64编码值,并将其直接包含在标头中,而不是通过auth=参数传递它们。 但是,这只能保护密码免受偶然的窥探者的攻击; 从编码值重建用户名和密码很容易。 Assuming this script runs interacti ...
  • 如何通过线路安全地将API密钥和秘密令牌传递给用户? 只有一种可靠的方法:为您的网站使用HTTP,以允许用户检索密钥。 然后在API调用期间不再需要HTTPS。 您的用户可以使用HMAC身份验证来使用共享密钥对密钥进行哈希处理。 how do I deliver the API keys and secret tokens securely over the wire to the users? There is only one reliable way: use HTTPs for your web s ...
  • 你应该比那更具防御性。 首先检查null 。否则你要求它爆炸: var entity = platypi.Find(p => p.Id == id); return entity == null ? string.Empty : entity.Name; 您目前还在进行多次查找...您不需要( Find以查找名称..然后Find返回名称..)。 You should be much more defensive than that. Check for null first.. otherwise yo ...
  • 我推荐使用模块node-env-file 。 这就是我用于我的项目,我很高兴。 设置非常简单(查看他们的文档)并且像魅力一样工作。 它从.env文件读取变量并将它们嵌入到Node项目中。 .env # your environment variables API_KEY1=abcd123 app.js var env = require('node-env-file'); var API_KEY1 = process.env.API_KEY1; I recommend using module node ...
  • 将API密钥保存在Web根目录之外的文件中。 然后将该文件包含在需要使用它的任何文件中。 通过将其放置在Web根目录之外,不能通过Web浏览器或其他类似方式直接访问它。
  • 您需要将脚本调用放在application.html.erb文件中,以便ruby可以解析ENV['google_api'] 。 这是一个示例应用程序文件(在HAML中): !!! %html %head %title Pursuit = stylesheet_link_tag 'application', media: 'all' = csrf_meta_tags :javascript var GOOGLE_API_KEY="#{ENV['GOOGLE_A ...
  • 安全始终是一种方便的权衡。 没有真正的绝对。 对于访问存储库和可能访问密钥的(潜在)git主机的任何人,您是否很酷? 所有开发人员的计算机都安全且使用磁盘加密吗? 密钥可以访问什么? AWS上的一切? 您是否相信能够访问回购的人足以保持安全并且不会意外地分享进一步的东西? 关键是你需要尝试完全理解秘密可能泄露的方式。 就个人而言,我宁愿将它们置于git之外,并通过usb棒或加密电子邮件将密钥分发给开发人员。 而且只针对需要它们的人。 只有我们开发人员的一部分实际上需要访问。 如果您确定风险过大,请确保更换所 ...
  • 您可以像这样设置文件夹目录: config.js var config = {}; //development var env = process.env.NODE_ENV || 'development'; if(env === 'development'){ config = require('./env/development'); }else if (env === 'test'){ config = require('./env/test'); } else if(env === 'p ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)