jQuery 1.4.4+ AJAX请求 - 发布空数组或对象变为字符串

Nat*_*rth 6 javascript ajax jquery post json

我在Javascript中有一个对象,我正在尝试将AJAX POST发送到PHP脚本.一切都在jQuery 1.4.1中工作,但现在在1.4.4或更高版本中,所有空数组或空对象都以字符串(0)形式出现,这是不正确的.

JS:

$(document).ready(function() {
var obj = {};
obj.one = [];
obj.two = {};
obj.three = [];
obj.three.push('one');
obj.three.push('two');
obj.three.push('three');
obj.four = "onetwothree";

$.ajax({
    type: 'POST',
    url: 'ajax.php',
    data: obj,
    success: function(data) {
        alert(data);
    },
});
});
Run Code Online (Sandbox Code Playgroud)

PHP:

<?php
var_dump($_POST);
?>
Run Code Online (Sandbox Code Playgroud)

响应:

array(4) {
  ["one"]=> string(0) ""
  ["two"]=> string(0) ""
  ["three"]=> array(3) {
    [0]=> string(3) "one"
    [1]=> string(3) "two"
    [2]=> string(5) "three"
  }
  ["four"]=> string(11) "onetwothree"
}
Run Code Online (Sandbox Code Playgroud)

在版本1.4.1中,它不会发送["one"]或["two"],但现在在较新的版本中,它以字符串形式到达的事实会抛出整个应用程序.有什么我可以做的,以便空数组([])作为空数组([])到达PHP并与JavaScript对象相同?

kar*_*m79 2

尝试将traditional选项设置为true

$.ajax({
    type: 'POST',
    traditional: true,
    url: 'ajax.php',
    data: obj,
    success: function(data) {
        alert(data);
    }
});
Run Code Online (Sandbox Code Playgroud)

查看较新 APIdata和选项。traditional

success如果您希望在 IE7 中正常工作,请删除回调后面多余的逗号。