Bug Tracker

Modify

Ticket #11097 (closed enhancement: wontfix)

Opened 17 months ago

Last modified 6 months ago

$.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:
Blocking: Blocked by:

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

comment:1 Changed 17 months ago by rwaldron

  • Priority changed from undecided to low
  • Resolution set to wontfix
  • Status changed from new to closed
  • Component changed from unfiled to ajax

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

comment:2 Changed 14 months 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 14 months 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 14 months 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 6 months ago by dmethvin

#12935 is a duplicate of this ticket.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.