简体   繁体   English

在现有的 JSON 数组中添加 Javascript Object

[英]Adding Javascript Object inside existing JSON Array

Apologies if this seems obvious, but I've been lost on it for a few hours now.抱歉,如果这看起来很明显,但我已经迷失了几个小时。

I'm attempting to create a javascript object and pass it to a PHP page using JQuery's.ajax.我正在尝试创建一个 javascript object 并将其传递给 PHP 页面使用 JQuery's.Z2705A83A39A0659EDA5264。

The JS object looks like: JS object 看起来像:

let transactions = {};
let counter = 0;
($.each loop) {
    transactions[counter] = {
        "code": code,
        "agency_number": agency_number,
        "agency_code": agency_code
    };
    counter++;
}

I then need to add it, along with another field to a JSON array然后我需要将它与另一个字段一起添加到 JSON 数组

let formData =  [{}];
    formData.push({name: 'page', value: aPage});
    formData.push({name: "transactions", value: JSON.stringify(transactions)});

The AJAX Call like: AJAX 调用如下:

let ajax_call=$.ajax({
                datatype: "json",
                method: "POST",
                url: "theURL",
                data: formData,
                timeout: 15000,
                async: true
            });
            ajax_call.done(function( data, textStatus, jqXHR ) {
               ... blah ...

wrapping transactions in JSON.stringify doesn't seem to work.在 JSON.stringify 中包装事务似乎不起作用。 On the PHP end, the POST value for transaction looks like:在 PHP 端,事务的 POST 值如下所示:

"{0:{CODE:300,AGENCY_NUMBER:20,AGENCY_CODE:50}}"

No ticks, which doesn't allow me to use json_decode.没有滴答声,这不允许我使用 json_decode。 It outputs "syntax error" as expected.它按预期输出“语法错误”。

passing it just as transactions, without the JSON.stringify yields the result:将其作为事务传递,没有 JSON.stringify 会产生结果:

formData.push({name: "transactions", value: transactions});

=

"[OBJECT OBJECT]"

which is a string representation of that, not an actual object to work with.这是一个字符串表示,而不是实际的 object 可以使用。

I'm not doing anything with the POST values up until this point.到目前为止,我没有对 POST 值做任何事情。 This is how the PHP page is receiving them.这就是 PHP 页面接收它们的方式。

Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks!谢谢!

Edit - There wasn't actually anything wrong with the code here.编辑 - 这里的代码实际上没有任何问题。 I was restricting quotation marks, along with other special characters, from hitting the web server in IIS.我限制引号以及其他特殊字符在 IIS 中访问 web 服务器。 That's why it was coming through without them.这就是为什么它在没有他们的情况下得以实现。 All is well, I am just dumb.一切都很好,我只是愚蠢。

Thank you all for the assistance otherwise.否则谢谢大家的帮助。

JQuery ajax.data can accept regular JS objects. JQuery ajax.data 可以接受常规的 JS 对象。 You don't have to create name:value pairs and stringify anything.您不必创建名称:值对和对任何内容进行字符串化。

This should work better:这应该会更好:

const formData = {
    page: aPage,
    transactions: []
};

($.each loop) {
    formData.transactions.push({
        "code": code,
        "agency_number": agency_number,
        "agency_code": agency_code
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM