Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#11097 closed enhancement (wontfix)

$.param should allow custom URI encoding

Reported by: Ramses Vidor <ramsvidor@…> Owned by:
Priority: low Milestone: None
Component: ajax Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:

Description

$.param uses the JavaScript encodeURIComponent() function for encoding request params, which has a few issues when not encoding UTF-8 characters in AJAX requests.

One of the issues occurs when encoding ISO-8859-1 and sending an AJAX request to a Java backend using Struts 2. The backend doesn't recognise the URI encoding with this function, but works well with the JavaScript function escape().

Instead of using the encodeURIComponent() directly, it would be better to use a custom jQuery module, (i.e. $.encodeURI), which could be overwritten in the cases where encodeURIComponent doesn't work properly.

Example:

Implement:

// Case 1 - doesn't affect current implementation
$.fn.encodeURI = function( uri ) {
    return encodeURIComponent( uri );
};

And change the line 7602 (jQuery 1.7.1) to:

s[ s.length ] = jQuery.encodeURI( key ) + "=" + jQuery.encodeURI( value );

Then it could be easily overwritten to:

// Case 2 - overwritten to be compatible
// with ISO-8859-1 and Java/Struts 2 backend
$.fn.encodeURI = function( uri ) {
    return escape( uri );
};

Change History (5)

comment:1 Changed 8 years ago by Rick Waldron

Component: unfiledajax
Priority: undecidedlow
Resolution: wontfix
Status: newclosed

This can easily be monkey patched and offered as a plugin.

comment:2 Changed 7 years ago by anonymous

I am having the same charset issue with encodeURIComponent. How do I could change this behavior without overwriting the whole method?

comment:3 Changed 7 years ago by dmethvin

Since encodeURIComponent is a standard JavaScript function your best bet would be to use UTF-8. Otherwise write yourself a patch or plugin. The forums would be a better bet for getting a specific solution.

comment:4 Changed 7 years ago by anonymous

According to Mozilla:
escape(): "The escape and unescape functions do not work properly for non-ASCII characters and have been deprecated. In JavaScript 1.5 and later, use encodeURI, decodeURI, encodeURIComponent, and decodeURIComponent."

encodeURIComponent(): "Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters)."

Looks like this is why jQuery uses encodeURIComponent instead of escape. Unfortunately this kind of thing breaks old apps written with other charsets instead of UTF-8.

The suggested solution above would be nice, cause it allow you to override only the code portion that escapes the parameters. So we wouldn't need to rewrite all the jQuery.param behavior into a new plugin.

comment:5 Changed 7 years ago by dmethvin

#12935 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.