Firefox won't let me send '&' with AJAX!

As a complement to my previous I should mention that one should
generally the encodeURIComponent() function for escaping the content one
wishes to send. So instead of esacping the “&” or anything like it one
can simply escape the whole content like:

content = encodeURIComponent( content );

Cheers!

Hi!

I am writing to reply to the first post. I am Greek so html entities are
crucial in web development for me too! I understand exactly what the
problem is. When one tries to send html entities that contain the “&”
(ampersand) the data part of the send() method of the XMLHttpRequest
breaks, because this is the character that separates url fields from
each other.

To my mind the send method ought to have two ways of sending data! The
one that it already has for GET methods and another one with an
unspecified number of possible fields in case one wants to send a POST
method. It is totally silly - I believe - to send a POST by saying:

xml_http_request.send( "name=Takis&age=27" );

This is the classic way for doing this for a GET and there are good
reasons for it, so why use it in a POST? I will never understand it! Now
I cannot really understand how the other people here do this or the way
they proposed - that seems to work for the original poster. Either I
don’t get or I am too dizzy searching for a solution for quite some time
now.

So I thought of a plain solution. How are “&” sent in a GET usually?
They are escaped! They are escaped by “%26”! So this is what I believe
one should do when sending large texts - like the ones that online rich
text editors (ie FCK, etc) produce - via an XMLHttpRequest send() POST!

That is until the people that run the internet (lol) invent a new send()
method for XMLHttpRequest like:
xml_http_request.send( {
name: “Takis”,
age: “27”,
last_words: “I want to send an & with AJAX!”
} );

Bye…

Hey,

I’ve got the same problem when I use an ampersand in filenames, I just
get an xml error message and nothing is working anymore.

But I’ve also got another problem with parenthesis/brackets like
“folder(2006)”. I’m programming an online file_browser and I can’t enter
directories by link_to_remote which have a name with brackets or even
contain a file which is named with brackets. I always get an error
message that says, that one “)” is missing.

So is there any way to escape all special characters and signs for a
whole document? Also the content-type handling doesn’t affect anything,
neither self made escape-functions with h(), because in some cases I
think I need an “ä” converted to ä in other cases to &#228? Is that
possible or am I just to blind to see the solution? Any help? Pleeeaaase
=)
Thanks in advance!

Nina

Hi Nina,

nina wrote:

But I’ve also got another problem with parenthesis/brackets
like “folder(2006)”. I’m programming an online file_browser
and I can’t enter directories by link_to_remote which have a
name with brackets or even contain a file which is named with
brackets. I always get an error message that says, that one “)”
is missing.

If you post some code that’s causing you problems, maybe we can help.
As
far as the problem above, link_to_remote invokes a controller method.
At
first blush, it doesn’t sound like you’re using it correctly, but code
and
an explanation of what you’re trying to accomplish will probably help.

Best regards,
Bill

I use the following snippet in a before_filter to clean up params
affected by this encoding ‘feature’:

URI: /admin/en/products_extras?extra_id=1&product_id

Filter snippet:

def clean_params_proxy(*to_clean)
to_clean.map{|p| params[p] = params[“amp;#{p.to_s}”] if
params.has_key?(“amp;#{p.to_s}”)}
end

Usage:

class Admin::ProductExtrasController < Admin::BaseController

before_filter :clean_params, :only => :create

def create; end

def destroy; end

protected

def clean_params
clean_params_proxy :product_id, :extra_id
end

end

  • Lourens

As a complement to my previous I should mention that one should
generally the encodeURIComponent() function for escaping the content one wishes to send. So instead of esacping the “&” or anything like it one can simply escape the whole content like: content = encodeURIComponent( content );
just a quick note… that’s done behind the scenes when you use
Element.serialize (or Form.serialize) in prototype.js, and it’s the
method that is automatically called when you use the rails remote
methods defined in the helpers.

the only thing you should be careful about is encodeURIComponent by
design will always send the data in UTF-8, whatever the encoding of your
original page was, so take it into account server side or you’ll end up
with strange characters.

regards,

javier ramirez

Estamos de estreno… si necesitas llevar el control de tus gastos
visita http://www.gastosgem.com !!Es gratis!!

Lourens Naude ha scritto:

end

On 2007/04/13, at 14:01, Bill W. wrote:

name with brackets or even contain a file which is named with

I don’t understand what is the problem but I see a formal error amp;
instead of &

Correct this

def clean_params_proxy(*to_clean)
to_clean.map{|p| params[p] = params[“amp;#{p.to_s}”] if
params.has_key?(“amp;#{p.to_s}”)}
end

into

def clean_params_proxy(*to_clean)
to_clean.map{|p| params[p] = params["&#{p.to_s}"] if
params.has_key?("&#{p.to_s}")}
end

Hope this help and have fun !