json某字段是一段html,javascript – 破坏JSON的HTML字符串( )

我从我的页面收集一些数据,将数据存储在一个数组中,以便在页面上进行多次使用,并通过

AJAX发送数组副本,将数据存储在

PHP页面的数据库中.

我存储在数组中的一个数据是TinyMCE WYSIWYG编辑器的输出,因此它包含HTML,但刚刚发现这是一个问题 – 我将解释:

在我的WYSIWYG编辑器中输入一行文本并触发我的AJAX事件后,这是我的控制台中显示的JSON字符串,一切正常,数据库已发送并存储:

{"id":"229","topic":"","title":"","description":"","content":"

fgfgfdg.

","keywords":""}

如果我写两行文本,这是JSON字符串并且成功:

{"id":"229","topic":"","title":"","description":"","content":"

fgfgfdg.

\n

fgfgfdg

","keywords":""}

现在,如果我写了一行文字并按回车而没有在第二行输入任何内容,我会得到以下内容失败.

{"id":"229","topic":"","title":"","description":"","content":"

fgfgfdgdfgdfgdfgdfg.

\n

好像& nbsp;以某种方式打破了我的JSON输出.我的PHP无法访问已解码的数组值,因为没有数组. print_r(json_decode($json))什么都不返回.有人可以帮忙吗?

这是我的jQuery的HTML页面:

var post_data = {};

post_data.id = post_id;

post_data.topic = topic;

post_data.title = title;

post_data.description = description;

post_data.content = tinyMCE.activeEditor.getContent();

post_data.keywords = keywords;

post_data = JSON.stringify(post_data);

save_post_request = $.ajax({

url: 'ajax/save-post.php',

type: 'POST',

data: "save_mode="+save_mode+"&post_data="+post_data,

dataType: 'text',

cache: false

});

这是我的PHP页面:

header('Content-type: application/json; charset=UTF-8');

$post_data = isset($_POST['post_data']) ? $_POST['post_data'] : null;

$post_data_arr = json_decode($post_data, true);

$post_id = $post_data_arr['id'];

$topic = $post_data_arr['topic'];

// others

$content = $post_data_arr['content'];

if (!$post_data_arr['id']) {

// fails here

// id is not accessible when the JSON contains

}

这就是Firebug所说的:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值