1

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?

1
  • 5
    Are you really using jQuery 1.3 ? Commented Nov 5, 2012 at 14:44

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

it's not quite the same - when using jquery 1.7 it seems to send a request parameter getBlocks in the example above
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.