0

i have a string passed from php script to jquery script encoded with rawurlencode():

$str = '<test>';
echo rawurlencode($str);

and i'm trying to decode it with decodeURIComponent:

var decoded = decodeURIComponent(str);

finally i'm getting "%3Ctest%3E" anyway. (I have tried urlencode() with the same result aswell.) What am i doing wrong ?

Serg
  • 11
  • 3

1 Answers1

0

PHP side :

$str = htmlentities('<test>');
header("Content-type: application/json");
echo json_encode($str);

Javascript side :

var decoded = JSON.parse(answer);
$('#ui-tabs-1').append(decoded);

In such a way, you'll be able to pass arrays or objects from PHP to your JS code too.

More info here

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
  • `var decoded = JSON.parse(answer);` `$('#ui-tabs-1').append(decoded);` shows """". firebug shows response "test". – Serg Sep 13 '12 at 21:24
  • hmm, echo json_encode(htmlentities($str)); may work if you want to insert result to the dom. – Alain Tiemblo Sep 13 '12 at 21:27