1

Possible Duplicate:
JSON: why are forward slashes escaped?

I am passing values which contain "//" from PHP to Javascript. However the values keep parsing "//" as: "\/\/" when I decode using json, no matter how I try to enclose or escape the strings... .

Here's the code I'm using:

PHP

foreach($varr as $vr)
{
    array_push($legendarr, "%%.%% - ".$vr);                        
    array_push($linkarr, "http://".$_SERVER['HTTP_HOST']."/getdata.php?criteria=".$vr);
}

JavaScript:

pie = r.piechart(320, 240, 100, <?php echo json_encode($vcr); ?>, { legend: <?php echo json_encode($legendarr); ?>, legendpos: "west", href: <?php echo json_encode($linkarr); ?>});

The problem is that the variable linkarr is displayed in the form:

http:\/\/localhost:8090\/getdata.php?criteria=......

Could anyone know why this is happening? How can I fix it?

Thanks.

Community
  • 1
  • 1
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • In PHP5.4+ you could use the `JSON_UNESCAPED_SLASHES` option. As mentioned it's generally unneeded for JS though. (Also available in upgradephp `up_json_encode()` for earlier PHP versions.) – mario Aug 14 '12 at 02:37

1 Answers1

1

A string "http:\/\/www.example.org" will still be printed as "http://www.example.org" in JavaScript.

The escaped slashes is, I believe, part of the JSON standard. But it shouldn't affect your JS code in any way.

Btw, the mysql_escape_string('//') is completely out of context; HTML != database.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309