【问题标题】:JSON - jquery to php; how to POST dataJSON - jquery 到 php;如何发布数据
【发布时间】: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 可打印字符串。您的帖子数据来自哪里?尝试在表单上使用 serializeserializeArray
  • 你能记录你的:JSON.stringify(postData)。如果发送一个json对象,php会像数组一样理解(如果是js对象)

标签: php jquery symfony


【解决方案1】:

删除htmlspecialchars_decode

用途:

$content = html_entity_decode($this->get('request')->getContent());
$data = json_decode($content);

所有问题都是因为JSON.stringify(postData)。你真的需要这个吗?

【讨论】:

  • 他在问题中提到他“尝试”了htmlspecialchars_decode。这应该是一条评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
相关资源
最近更新 更多