jquery - jQuery 1.3 中的 post() 将对象传输为 [object Object]

标签 jquery jquery-1.3

我有以下代码

// data is some object
var data = {
    getBlocks:{}
};

data['foo'] = 'bar';

jQuery.post(
    'http://example.com/foo.php',
    data,
    function (response) {
         ....
    },
    'json'
);

在服务器端,数据显示为“[object Object]”。在最近的 PHP 版本中,它运行良好。

不幸的是,由于存在大量遗留 JQuery 代码,目前无法进行升级。

我该如何解决这个问题?

最佳答案

使用JSON.stringify()将对象转换为JSON:

jQuery.post(
    'http://example.com/foo.php',
    JSON.stringify(data),
    function (response) {
         ....
    },
    'json'
);

根据浏览器的不同,您可能需要添加 JSON函数(请参阅 json2.js)。

关于jquery - jQuery 1.3 中的 post() 将对象传输为 [object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13234376/

相关文章:

jquery - 使用 jQuery 将变量解析为 JSON

javascript - 基本 jQuery slider 在页面加载时无法正确显示(响应式设置)

javascript - 使 JQuery 按钮显示为已按下;即改变主题

jQuery 选择器问题

jquery - 如何为 jQuery 对象分配属性?

javascript - 将 jQuery.parseJSON 添加到 jQuery 1.3

javascript - 如何将输入字段的值获取到多个对象的数组中

jquery定时弹出

jquery - 将多个 jquery 循环与 ipad 的 touchwipe(滑动)事件结合使用

javascript - jQuery 事件处理程序未在 IE 中触发