Alex
Alex

Reputation: 34978

post() in jQuery 1.3 transmitting object as [object Object]

I have the following code

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

data['foo'] = 'bar';

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

On the server side, the data appears as "[object Object]". In recent PHP versions it is working fine.

Unfortunately upgrading is not an option in the moment due to lots of legacy JQuery code.

How can I fix this?

Upvotes: 1

Views: 252

Answers (2)

Alex
Alex

Reputation: 34978

JSON encoding is not working without changing the server side code.

A solution is to backport the required functionality from 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, "+" );
}

And before the post:

data = jQuery16param(data);

Upvotes: 1

PiTheNumber
PiTheNumber

Reputation: 23542

Use JSON.stringify() to convert the object to JSON:

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

Depending one the browser you might need to add JSON functions (see json2.js).

Upvotes: 2

Related Questions