7*4 2015-04-18 17:18 采纳率: 50%
浏览 35

jQuery的.ajax是否正在丢失数据?

Okay I'm fiddling around with this for hours and getting frustrated. I want to build an application form with ajax. I pass an associative array with huge nested arrays in it:

[
    "foo": x,
    "bar": y,
     // ...
]

Here is an example what x or y can look like (it's way too huge to post it here). It's basically just an object with nested arrays in it:

{
    lastModified: 1243245656
    some: x
    other: y
    keys: z
    feed: [ ... ]
    items: [ ... ]
    // ...
}

So my associative array added_toons would like this:

[
    "foo": {
        lastModified: 1243245656
        some: x
        other: y
        keys: z
        feed: [ ... ]
        items: [ ... ]
        // ...
    },
    "bar": {
        lastModified: 1243245656
        some: x
        other: y
        keys: z
        feed: [ ... ]
        items: [ ... ]
        // ...
    },
    // ..
]

Here comes the problem: When I evaluate my array right before I pass it with ajax everything is fine. As soon as I receive it in my php backend there are missing arrays within each toon. items and feed for example.
I really have no idea what is happening.

So I'm asking for some help to troubleshoot this. AFAIK it shouldn't be a problem with escaping since jQuery already does this for me.


My JS code

    console.info(added_toons); // everything is fine here
    $.ajax({
        url: '...',
        type: 'post',
        data: {
            'action': 'submit',
             // ...
            'toons': added_toons
        },
        success: function (data, status) {
            // ...
        },
        error: function (xhr, desc, err) {
            // ..
        }
    });

My PHP code

<?php
if ($_POST['action'] == 'submit') {
    // when I here evaluate $_POST it contains `toons` 
    // but each `toon` is missing keys
}
  • 写回答

1条回答 默认 最新

  • weixin_33743703 2015-04-18 17:25
    关注

    Try this way to set JSON.stringify for your added_toons

    data: {
            'action': 'submit',
             //stringify is important to send array of objects
            'toons': JSON.stringify(added_toons) 
          },
    

    See about JSON.stringify() here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染