简体   繁体   English

和字符中断通过PHP接收JSON

[英]& character breaks received json by php

I have an array with more objects. 我有一个包含更多对象的数组。 If one object contains & character, every object after the & is not received by php. 如果一个对象包含&字符,则php不会接收&之后的每个对象。 What might be the problem? 可能是什么问题?

THIS IS THE AJAX 这就是阿贾克斯

xmlhttp.open("POST", "get.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send('data='+JSON.stringify(x));

THIS IS THE PHP 这就是PHP

$json = $_POST['data'];
echo $json;

When entered 输入时

a&b

I got 我有

[[{"x":x,"y":"x","z":"z"}],[{"w":a

您还必须使用encodeURIComponent函数:

xmlhttp.send('data=' + encodeURIComponent(JSON.stringify(x)));

The problem is that stringify does not escape special characters like the & sign. 问题在于stringify不能转义&符号等特殊字符。 You should use encodeURIComponent(JSON.stringify(x)) 您应该使用encodeURIComponent(JSON.stringify(x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM