Forum: Ruby on Rails Firefox won't let me send '&' with AJAX!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-12 01:13
Hi everyone,

I've encountered a HUGE problem, which may not be RoR-oriented, but
there might be a workaround somehow.

See, at this moment, I'm unable to send AJAX requests back and forth
when the information contains an ampersand ('&'). In essence, request is
never completed. The odd thing is that it only happens with Firefox (IE
and Opera works fine).

This means that I'm unable to pass along html entities, since they all,
conveniently enough, contains ampersands. I can't replace the ampersand
with the html entitiy for it, either, since that would also contain an
ampersand. Being Swedish, I'm kinda screwed if I can't use å,
ä and ö on my website. Replacing them with the letter without
using entities is out of the question, too; they only come out as nice
boxes if I do. :)

Please note that the request itself can be without ampersands, and it
still doesn't work as long as the data I fetch contains one or more
ampersands.

Does anyone have a clue on how to fix this, or work around it? I've
tried creating a function which substitutes certain characters with
their entity-form using string.gsub(), but that doesn't work either. Is
there a way to perform such a substitution after the AJAX-request is
completed, and if so, how do I do that?

Thanks in advance,

Sebastian
54532f023496410e0d7b1add5561ba45?d=identicon&s=25 Manuel Holtgrewe (Guest)
on 2006-04-12 01:17
(Received via mailing list)
Hi, Sebastian.

Could you give us "the simplest thing that works", i.e. the "simplest
thing that breaks" in your case? It will be hard to find someone who
has the same problem when you describe it abstractly but very easy if
you give an example :)

Regards,

Manuel
3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 Mark Van Holstyn (Guest)
on 2006-04-12 01:21
(Received via mailing list)
I'm sure there is a helper within RoR that will do this for you, but you
need to escape certain characters in urls. See
http://www.december.com/html/spec/esccodes.html for what the escape
sequences are (for example, "&" is "%26"

--mark
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-12 01:45
Manuel Holtgrewe wrote:
> Could you give us "the simplest thing that works", i.e. the "simplest
> thing that breaks" in your case? It will be hard to find someone who
> has the same problem when you describe it abstractly but very easy if
> you give an example :)

Manuel, I'm not really sure how to give an example of it; I've used
link_to_remote to fetch information from a database - nothing strange or
alien.

Controller:

@staff = Staff.find(:first, :conditions => [ "name = ?", params[:name]])
render :partial => "staff"

However, as *soon* as the information in any way contains an ampersand,
it ceases to function. For example, if my partial looks like this:

<%= @staff.name %> <%= @staff.position %> <%= @staff.email %>

..it will work without a problem for certain entries (ex. @staff.name ==
Foo Bar), but others will fail (ex. @staff.name == Foo Bar&aacute;),
only because that specific database column contained an ampersand.
Omitting <%= @staff.name %> from the partial makes it work again,
though.

Again, only in Firefox. It works with ampersands in IE and Opera.

Mark Van Holstyn wrote:
> I'm sure there is a helper within RoR that will do this for you, but you
> need to escape certain characters in urls. See
> http://www.december.com/html/spec/esccodes.html for what the escape
> sequences are (for example, "&" is "%26"

Mark, the problem does not appear to be in the urls, but rather the data
fetched from the database, so I'm not sure if escaping certain
characters in url would work.

Best regards,

Sebastian
3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 Mark Van Holstyn (Guest)
on 2006-04-12 02:40
(Received via mailing list)
If you are sending "&" in the url as data, you will have problems
because
"&" is what url's use to split fields. If you aren't sending "&" in the
url,
them I am not sure what the porblems you are having is, in which case I
would agree with Manuel, I think we need an example.

--mark
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 seth b. (subimage)
on 2006-04-12 03:07
(Received via mailing list)
So use POST instead of GET...

problem solved.
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-12 17:05
Mark Van Holstyn wrote:
> If you are sending "&" in the url as data, you will have problems
> because
> "&" is what url's use to split fields. If you aren't sending "&" in the
> url,
> them I am not sure what the porblems you are having is, in which case I
> would agree with Manuel, I think we need an example.
>
> --mark
Okay, full example:

# template #

<%= link_to_remote("Technology used",
    :update => 'm4-right-wrap-projdesc',
    :url => { :action => :projtech, :id => @project.id },
    :loading => "Element.hide('m4-right-wrap-projdesc')",
    :complete => { visual_effect(:appear, 'm4-right-wrap-projdesc'),
visual_effect(:appear, 'm4-right-proj-glossary')}) %>

# template render for id #1 #

<a href="#" onclick="new Ajax.Updater('m4-right-wrap-projdesc',
'/portfolio/projtech/1', {asynchronous:true, evalScripts:true,
onComplete:function(request){new
Effect.Appear('m4-right-wrap-projdesc',{});new
Effect.Appear('m4-right-proj-glossary',{});},
onLoading:function(request){Element.hide('m4-right-wrap-projdesc')}});
return false;">Technology used</a>

# controller #

def projtech
  @project = Project.find(params[:id])
  render :partial => "tech"
end

# partial #

<%= @project.technology %>

# end #

This doesn't work because in my MySQL table called "projects", the
column "technology" for id #1 contains an ampersand. As you can see, I
don't have & in the url, which suggests that that's not the problem.

So, basically, this doesn't work in Firefox because the data I'm
*fetching* in the MySQL table contains an ampersand (or more). It has
nothing to do with the url. Try it yourselves - it won't work in Firefox
1.5.

I also tried adding :method => 'post' to the link_to_remote, but that
didn't change anything.

Regards,

Sebastian
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-12 17:14
On second thought, let's make the example even easier:

Create a link_to_remote; call it whatever you wish and update whichever
id of your choice. Create an action to call, call that one whatever you
wish. In the action you call, simply have:

def action_to_be_called_by_ajax
  render_text "&auml;"
end

...and it won't work in Firefox. Remove the ampersand in the text to be
rendered and it will.

- Sebastian
3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 Mark Van Holstyn (Guest)
on 2006-04-12 18:04
(Received via mailing list)
Your last example works for me and displays...
ä

The only thing i can think of is if your firefox cannot display
non-acsii
characters, however my firefox definately works.

--mark
09ff21c336ae126e8b3199f8b64d7796?d=identicon&s=25 D. Taylor Singletary (Guest)
on 2006-04-12 18:07
(Received via mailing list)
Don't forget that Rails has the h(string) helper method availabile in
your
view.

So when you want to show one of your problematic data pieces in a view
you'd
do this

<%= h("This & That") %>

This might solve your problem.
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-12 19:03
Mark Van Holstyn wrote:
> Your last example works for me and displays...
> ä
>
> The only thing i can think of is if your firefox cannot display
> non-acsii
> characters, however my firefox definately works.
>
> --mark

That's very strange, since it doesn't work for me, or for several people
I've asked. Maybe you're using another version of Firefox? I have
1.5.0.1.

- Sebastian
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-12 19:09
D. Taylor Singletary wrote:
> Don't forget that Rails has the h(string) helper method availabile in
> your
> view.
>
> So when you want to show one of your problematic data pieces in a view
> you'd
> do this
>
> <%= h("This & That") %>
>
> This might solve your problem.

This is useful, but unfortunately didn't solve this problem. Now the
data is displayed and everything, but as the entity (&auml;) instead of
the symbol it represents.

Another strange thing I've noticed is that &amp;auml; works (in other
words when I substitute & for &amp;). That also outputs &auml; instead
of the symbol, though, but I'm currently investigating a method of
working around the problem.

- Sebastian
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-12 20:28
Sorry to bump this, but it seems that the method I was trying out didn't
work. Basically, if someone could figure out some client-side way to
replace characters (&amp; with & in this case), I'd be eternally
grateful. Rails-code won't work, since that's server-side. JavaScript
doesn't appear to work, either, at least not the way I tried (writing a
function and calling it from the partial). Can it be done with RJS,
somehow? I was for a split-second thinking that replace_html could help,
but then I realised that's only for id's and doesn't work as a global
find/replace. Besides, that's server-side, as well.

I also tried printing the symbols directly and forcing a special
encoding, but that wasn't reliable at all...

Again, I'm very, very thankful for any help on the topic. This is
literally the last thing left to do on a major site for me, and I'm
already past the deadline.

Regards,

Sebastian
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2006-04-12 22:04
(Received via mailing list)
2006/4/12, Sebastian von Conrad <sebastian@march4.se>:
>
> Again, I'm very, very thankful for any help on the topic. This is
> literally the last thing left to do on a major site for me, and I'm
> already past the deadline.
>

If you replace the link_to_remote with a link_to, does it display
properly? (IOW: Is it only realted to XMLHttpRequest? Can you pass you
data through XMLHttpRequest or is it a prototype problem?)

Douglas
3bef3fc4d0cbcf58f3d66c106e2fd16f?d=identicon&s=25 Mark Van Holstyn (Guest)
on 2006-04-12 23:17
(Received via mailing list)
I am running Firefox 1.5.0.1 as well. If you do something like this...

def action_to_be_called_by_ajax
 render_text "here is the character: &auml; :there was the character"
end

What is displayed? anything? "here is the character:  :there was the
character"?

--mark
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-13 00:14
Douglas Livingstone wrote:
> If you replace the link_to_remote with a link_to, does it display
> properly? (IOW: Is it only realted to XMLHttpRequest? Can you pass you
> data through XMLHttpRequest or is it a prototype problem?)
>
> Douglas

It's only related to XMLHttpRequest; link_to works fine.

Mark Van Holstyn wrote:
> I am running Firefox 1.5.0.1 as well. If you do something like this...
>
> def action_to_be_called_by_ajax
>  render_text "here is the character: &auml; :there was the character"
> end
>
> What is displayed? anything? "here is the character:  :there was the
> character"?
>
> --mark

Nothing at all happens. Or, well, the request is processed but never
finished. In other words, everything I specify under :loading is
executed, but :complete never is.

Regards,

Sebastian
B3260ee62969961010117e21e9872a3a?d=identicon&s=25 Kenneth Lee (Guest)
on 2006-04-13 00:46
(Received via mailing list)
Silly question but is &auml; considered a legal entity in XML ?
3e9065cf3439ac9b62e8cbe90c152cc3?d=identicon&s=25 Sebastian von Conrad (Guest)
on 2006-04-13 01:27
Kenneth Lee wrote:
> Silly question but is &auml; considered a legal entity in XML ?

That's is such a ridiculous cause of a problem.

Wow, do I feel stupid, or what? 24 hours of cry and dispair, and it
never even occured to me once - not once - to try &#228; instead of
&auml;. I'm getting blasé about this whole thing, I believe.

Thanks, Kenneth. You spared me a lot of trouble and ridicule (though I
think I have received/will receive plenty of both).

- Sebastian
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2006-04-13 01:53
(Received via mailing list)
Hey Kenneth Lee, it appears to be a valid character if you're using
iso-88591 <http://www.w3.org/TR/REC-html40/sgml/entities.html...
as
your character encoding for your HTML file.  By doing the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1" />
    <title>Some Stuff</title>
  </head>
  <body>

  ...

  </body>
</html>

Peace,

-Conrad
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2006-04-13 02:18
(Received via mailing list)
Hi, I have used the following and it worked for both numberical and
character:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
">
<html xmlns="http://www.w3.org/1999/xhtml"
>
  <head>
    <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"
/>
    <title>Some Stuff</title>
  </head>
  <body>
    <h2>Using character entity:&nbsp;&nbsp;&auml;</h2>
    <h2>Using numerical entity:&nbsp;&nbsp;&#228;</h2>
  </body>
</html>

Peace,

-Conrad
03f0c9d58cdd50a2b3e0d5961b4da35d?d=identicon&s=25 Takis Bouyouris (Guest)
on 2007-03-27 12:08
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...
03f0c9d58cdd50a2b3e0d5961b4da35d?d=identicon&s=25 Takis Bouyouris (Guest)
on 2007-04-13 12:33
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!
1f2eadfb41362800ebc2cf211b91d0f7?d=identicon&s=25 javier ramirez (Guest)
on 2007-04-13 12:46
(Received via mailing list)
> 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!!
3ce99c941462a43db9bfe5a9581cac2a?d=identicon&s=25 nina (Guest)
on 2007-04-13 14:42
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 &auml; 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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-04-13 15:02
(Received via mailing list)
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
58e87dd3aaaecd0d99a59985e3ca49f3?d=identicon&s=25 Lourens Naude (Guest)
on 2007-04-13 15:18
(Received via mailing list)
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
110e527af05961700d9e39285d043b62?d=identicon&s=25 Gianluca Tessarolo (Guest)
on 2007-04-13 15:32
(Received via mailing list)
Lourens Naude ha scritto:
>    end
>
> On 2007/04/13, at 14:01, Bill Walton 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 &amp;

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["&amp;#{p.to_s}"] if
params.has_key?("&amp;#{p.to_s}")}
   end

Hope this help and have fun !
This topic is locked and can not be replied to.