easyXDM and prototype.js, how to make it work

This is no longer needed from v2.1.0 as easyXDM now feature tests for a valid JSON implementation.

Cross-domain RPC was made easy by the introduction of easyXDM, but there are still obstacles to be found, especially when using libraries like PrototypeJs due to it extending the browser in unsound ways. Specifically, when it comes to PrototypeJs, ┬áit augments the Array objects prototype with an .toJSON method, something that renders the native/standardized JSON object crippled. This can easily result in deserialization errors, especially when it comes to arrays and one often sees messages like ‘params.push is not a function‘ and the like.

To work around with the easyXDM.Rpc class you can do the following:

xhr = new easyXDM.Rpc({
}, {
    // If we detect prototype.js then we use its implementation, if not JSON
    serializer: Object.toJSON ? {
        stringify: function(obj){
            return Object.toJSON(obj);
        parse: function(string){
            return string.evalJSON();
    } : JSON,
    remote: {

With this code we either use prototype for doing the serializing, or the native JSON object, but not a combination.

This entry was posted in Blog and tagged , , , , , , , . Bookmark the permalink.