试图发送<a> that includes a serialized PHP array through JSON-encoded AJAX

时间:2016-04-11 16:54:02

标签: php json ajax serialization escaping

I have a link that looks like this being generated in a php file called through AJAX:

<a href="/path/to/my/file.php?data=a:4:{s:4:"var1";i:1;s:4:"var2";i:2;s:4:"var3";i:3;s:4:"var4";i:4;}"></a>

To get this:

<?php
$other_info = '<a href="' . $_SERVER['SCRIPT_NAME'] . '?data=' . serialize($my_array) . '">Download</a>');

$retval = array('link' => $other_info);

echo json_encode($retval);
?>

I'm having trouble handling this on the front end. I tried enclosing it like this:

$other_info = htmlspecialchars($other_info);

but I can't get it to come through in a way I can handle on the front. It seems to enclose it in an additional set of double-quotation marks. How can I escape it out?

EDIT: I forgot to add this line, I apologize: $retval = array('link' => $other_info); It has been added to the code.

1 个答案:

答案 0 :(得分:0)

我能够找到一种方法来做到这一点。在PHP方面:

$other_info = htmlspecialchars(serialize($_REQUEST));

并在javascript方面:

var oi = resp.link
oi = '<a href="/path/to/my/file.php?data=' + oi + '">Download</a>'

修复了它。