1

我有以下代码

// 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 代码,目前无法选择升级。

我怎样才能解决这个问题?

4

2 回答 2

2

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

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

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

于 2012-11-05T14:59:22.757 回答
1

如果不更改服务器端代码,JSON 编码将不起作用。

一个解决方案是从 Jquery 1.6.2 向后移植所需的功能

function jQuery16buildParams( prefix, obj, traditional, add ) {
    if ( jQuery.isArray( obj ) ) {
        // Serialize array item.
        jQuery.each( obj, function( i, v ) {
            if ( traditional || rbracket.test( prefix ) ) {
                // Treat each array item as a scalar.
                add( prefix, v );

            } else {
                // If array item is non-scalar (array or object), encode its
                // numeric index to resolve deserialization ambiguity issues.
                // Note that rack (as of 1.0.0) can't currently deserialize
                // nested arrays properly, and attempting to do so may cause
                // a server error. Possible fixes are to modify rack's
                // deserialization algorithm or to provide an option or flag
                // to force array serialization to be shallow.
                jQuery16buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v, traditional, add );
            }
        });

    } else if ( !traditional && obj != null && typeof obj === "object" ) {
        // Serialize object item.
        for ( var name in obj ) {
            jQuery16buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
        }

    } else {
        // Serialize scalar item.
        add( prefix, obj );
    }
}

var r20 = /%20/g;

function jQuery16param( a, traditional ) {
    var s = [],
        add = function( key, value ) {
            // If value is a function, invoke it and return its value
            value = jQuery.isFunction( value ) ? value() : value;
            s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
        };

    // Set traditional to true for jQuery <= 1.3.2 behavior.
    if ( traditional === undefined ) {
        traditional = jQuery.ajaxSettings.traditional;
    }

    // If an array was passed in, assume that it is an array of form elements.
    if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
        // Serialize the form elements
        jQuery.each( a, function() {
            add( this.name, this.value );
        });

    } else {
        // If traditional, encode the "old" way (the way 1.3.2 or older
        // did it), otherwise encode params recursively.
        for ( var prefix in a ) {
            jQuery16buildParams( prefix, a[ prefix ], traditional, add );
        }
    }

    // Return the resulting serialization
    return s.join( "&" ).replace( r20, "+" );
}

在帖子之前:

data = jQuery16param(data);
于 2012-11-08T16:24:05.733 回答