【问题标题】:Send json to php through a form通过表单发送json到php
【发布时间】:2013-03-01 03:40:27
【问题描述】:
$('<form method="post" action="login/makeLogin"><input type="hidden" name="fbphoto" value="'+image +
                    '"><input type="hidden" name="fbname" value="'+response.first_name +'"><input type="hidden" name="fbemail" value="'+response.email +'"><input type="hidden" name="fbid" value="'+response.id+
                    '"><input type="hidden" name="fbsurname" value="'+response.last_name +'"><input type="hidden" name="fbfriends" value="'+json +'"></form>').appendTo('body')

我使用 javascript 动态创建此表单。但是当我尝试将此表单发送到 php 函数时,当我尝试访问 fbfriends 属性时它返回 false 其余值工作正常。 fbfriends 的值是一个包含 json 的字符串,我使用它:

var json=JSON.stringify(response.friends);

如果我不对 json 进行字符串化,我在 php 函数中检索值时会得到一个字符串 [Object object]。如何通过表单发送 json。

【问题讨论】:

  • 你有你的问题的答案使用 JSON.stringify 因为否则 json 将转换为字符串 ising toString 方法
  • 我在使用 stringify 时得到错误
  • 你可以尝试转义引号JSON.stringify(response.friends).replace(/"/g, '\\"');
  • 它现在可以工作,但我必须 " 替换引号

标签: php javascript json forms


【解决方案1】:

您的表单仅包含隐藏项。为什么不直接通过 $.ajax 发布响应值呢?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多