Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape & in a POST request in jQuery?

i have an input element with & in value:

<input type="checkbox" value="Biografie, životopisy, osudy, Domácí rock&amp;pop" />

When i try sending it through an ajax request:

$.ajax({
    type: "POST",
    url: "/admin/kategorie/add/link.json",
    data: "id="+id+"&value="+value+"&type="+type,
    error: function(){alert('Chyba! Reloadněte prosím stránku.');}
});

the post data that actually gets sent is:

id: 1
pop:
type: e
value: Biografie, životopisy, osudy, Domácí rock

*Note that all the variables in data are defined and value contains $(thatInputElement).attr('value').

How can I escape the &amp; properly so that post field value would contain Biografie, životopisy, osudy, Domácí rock&amp;pop?

like image 547
cypher Avatar asked Jul 23 '10 14:07

cypher


People also ask

How do you escape yourself?

We can try to forget ourselves by becoming fully absorbed in an activity, for instance, watching a film, playing a game, or working on a project. Some people opt to take various drugs that can alter one's states of consciousness. Such techniques can provide a respite, but the effects are temporary.

How do people escape their reality?

There is no “best” activity to help you mentally escape reality; however, you should always do something that you enjoy. Some people may find the best way to mentally escape reality is to go skydiving or snorkeling, while others may enjoy playing a computer game or putting together a puzzle.

Is it okay to escape reality?

Taking a step back from reality is a very effective way of experiencing a few moments of respite to help cope with life's hardships. When escapism is helpful, it is when we take a moment to step out of our reality and experience a period of escape from the challenges of our lives.


2 Answers

You can set your data option as an object and let jQuery do the encoding, like this:

$.ajax({
  type: "POST",
  url: "/admin/kategorie/add/link.json",
  data: { id: id, value: value, type: type },
  error: function(){ alert('Chyba! Reloadněte prosím stránku.'); }
});

You can encode each value using encodeURIComponent(), like this:

encodeURIComponent(value)

But the first option much simpler/shorter in most cases :)

like image 74
Nick Craver Avatar answered Oct 10 '22 07:10

Nick Craver


Have you tried this syntax?

 data: {"id":id, "value": value, "type": type }
like image 29
John Fisher Avatar answered Oct 10 '22 06:10

John Fisher