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

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. :slight_smile:

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

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 :slight_smile:

Regards,

Manuel

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
URL Escape Codes for what the escape
sequences are (for example, “&” is “%26”

–mark

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

So use POST instead of GET…

problem solved.

Manuel H. 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 :slight_smile:

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 B.), but others will fail (ex. @staff.name == Foo B.á),
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 H. 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
URL Escape Codes 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

Mark Van H. 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

Technology used

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

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 “ä”
end

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

  • Sebastian

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.

Mark Van H. 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

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

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 (& 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

D. Taylor S. 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 (ä) instead of
the symbol it represents.

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

  • Sebastian

2006/4/12, Sebastian von Conrad [email protected]:

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

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: ä :there was the character”
end

What is displayed? anything? “here is the character: :there was the
character”?

–mark

Douglas L. 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 H. 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: ä :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

Silly question but is ä considered a legal entity in XML ?

Kenneth L. wrote:

Silly question but is ä 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 ä instead of
ä. 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

Hey Kenneth L., it appears to be a valid character if you’re using
iso-88591 http://www.w3.org/TR/REC-html40/sgml/entities.html#iso-88591
as
your character encoding for your HTML file. By doing the following:

Some Stuff

Peace,

-Conrad

Hi, I have used the following and it worked for both numberical and
character:

Some Stuff

Using character entity:  ä

Using numerical entity:  ä

Peace,

-Conrad