Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

XML and JSON get transformed into URL Encoded Form Data #199

Closed
tysonnero opened this Issue Oct 19, 2012 · 4 comments

Comments

Projects
None yet
4 participants

When doing a cross-domain AJAX call, JSON and XML data get converted into URL encoded form data... Why do this rather then sending in the format intended to receive?

headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "X-Requested-With": "XMLHttpRequest"
}
// convert the data into a format we can send to the server 
var pairs = [];
for (var key in config.data) {
    if (config.data.hasOwnProperty(key)) {
        pairs.push(encodeURIComponent(key) + "=" + encodeURIComponent(config.data[key]));
    }
}
var data = pairs.join("&");

I agree - this needs to be fixed. The conversion should be optional.

In jQuery's ajax function it checks to see if the data is already a string:

        // Convert data if not already a string
        if ( s.data && s.processData && typeof s.data !== "string" ) {
            s.data = jQuery.param( s.data, s.traditional );
        }

I feel like easyXDM should do the same.

Also see

http://stackoverflow.com/questions/13077607/how-can-i-send-an-javascript-object-array-as-key-value-pairs-via-an-ajax-post-wi

and

http://pastebin.com/0eusK2vr

Shamelessly stealing code from jQuery, I suggest that this be changed to something like:

if ( config.data && config.processData && typeof config.data !== "string" ) {
    // s.data = jQuery.param( s.data, s.traditional );
    // convert the data into a format we can send to the server 
    var pairs = [];
    for (var key in config.data) {
        if (config.data.hasOwnProperty(key)) {
            pairs.push(encodeURIComponent(key) + "=" + encodeURIComponent(config.data[key]));
        }
    }
    var data = pairs.join("&");
}

You'll need to add the processData part to the default configuration too:

// apply default values if not set
easyXDM.apply(config, {
    method: "POST",
    processData: true,
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "X-Requested-With": "XMLHttpRequest"
    },
    success: Function.prototype,
    error: function(msg){
        throw new Error(msg);
    },
    data: {},
    timeout: 10 * 1000
    }, true);

It still doesn't address the issue that easyXDM can't recursively process a JSON object into valid www-form-urlencoded syntax - see test at http://pastebin.com/0eusK2vr but at least with this update I would be able to encode it with jQuery's param function (commented out above) and disable the auto-encoding of an already-string based data that I would try and send with easyXDM.

Owner

oyvindkinsey commented Oct 26, 2012

The cors file was always intended as a basic training implementation that
devs would adapt to their needs, switching out the ajax methods and so on.
It already requires you to edit the file in order to get it working
properly with the acls.
So, I'm wondering if we should just switch to using jquery? It will make it
more straightforward to switch out..
On Oct 26, 2012 8:14 AM, "clarkewd" notifications@github.com wrote:

Shamelessly stealing code from jQuery, I suggest that this be changed to
something like:

if ( config.data && config.processData && typeof config.data !== "string" ) {
// s.data = jQuery.param( s.data, s.traditional );
// convert the data into a format we can send to the server
var pairs = [];
for (var key in config.data) {
if (config.data.hasOwnProperty(key)) {
pairs.push(encodeURIComponent(key) + "=" + encodeURIComponent(config.data[key]));
}
}
var data = pairs.join("&");
}

You'll need to add the processData part to the default configuration too:

// apply default values if not set
easyXDM.apply(config, {
method: "POST",
processData: true,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"X-Requested-With": "XMLHttpRequest"
},
success: Function.prototype,
error: function(msg){
throw new Error(msg);
},
data: {},
timeout: 10 * 1000
}, true);

It still doesn't address the issue that easyXDM can't recursively process
a JSON object into valid www-form-urlencoded syntax - see test at
http://pastebin.com/0eusK2vr but at least with this update I would be
able to encode it with jQuery's param function (commented out above) and
disable the auto-encoding of an already-string based data that I would try
and send with easyXDM.


Reply to this email directly or view it on GitHubhttps://github.com/oyvindkinsey/easyXDM/issues/199#issuecomment-9816252.

I'd vote for community support of it as part of the project since I've already had to build PUT support and config.doSerialize functionality for my proj and I'm sure many others have done the same.

@tysonnero tysonnero closed this Aug 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment