1

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?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
coptic bag
  • 19
  • 3

2 Answers2

7

You should let jQuery do the encoding for you :

$.ajax({
   url: someUrl, // <- no parameter here
   data: {action:'abc', parameter:'C&W'},
   ...
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

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

var url = "action=" + encodeURIComponent(action) + "&parameter=" + encodeURIComponent(param);
Community
  • 1
  • 1
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134