【发布时间】:2015-04-08 02:42:54
【问题描述】:
我有一个 JSON 对象 (postData) 我想发送到我的 PHP Web 服务。
$.ajax({
url: postLink,
type: 'POST',
data: JSON.stringify(postData),
complete: function () {
console.log('COMPLETE: tried to send json');
},
success: function (data) {
console.log("Success" + data);
if (data.success) {
// do something
}
},
error: function (error) {
console.log("ERROR" + error);
}
});
在 PHP (Symfony) 中,我是这样阅读的:
$content = htmlspecialchars_decode($this->get('request')->getContent(), ENT_NOQUOTES);
$data = array();
if (!empty($content))
{
$data = json_decode($content, true);
var_dump($content);
我在json_decode 上收到内部服务器错误。我相信这是因为(根据我的var_dump)JSON 正在通过网络服务看起来像这样:
"auth":{"username":" ...
我什至尝试添加 htmlspecialchars_decode 以将这些引用转换回 "'... but it still comes back as"`
我不知道该怎么办。因此,非常感谢任何想法。
【问题讨论】:
-
JSON.stringify创建一个 HTML 可打印字符串。您的帖子数据来自哪里?尝试在表单上使用serialize或serializeArray。 -
你能记录你的:JSON.stringify(postData)。如果发送一个json对象,php会像数组一样理解(如果是js对象)