coptic bag
coptic bag

Reputation: 19

Pass parameter containing `&` with AJAX

I am passing my string to PHP through AJAX using $.Ajax.

I'm trying to pass this string:

action=abc&parameter=C&W

The AJAX splits the C&W on the basis of &, so the request comes in this format:

$action = "abc";
$parameter = "C";

How can I pass it as C&W, without it being split into a different parameter?

Upvotes: 1

Views: 792

Answers (2)

Danny Beckett
Danny Beckett

Reputation: 20748

Using bog-standard JavaScript (no jQuery), you can use encodeURIComponent:

var url = "action=" + encodeURIComponent(action) + "&parameter=" + encodeURIComponent(param);

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382150

You should let jQuery do the encoding for you :

$.ajax({
   url: someUrl, // <- no parameter here
   data: {action:'abc', parameter:'C&W'},
   ...

Upvotes: 7

Related Questions