我从我的页面收集一些数据,将数据存储在一个数组中,以便在页面上进行多次使用,并通过
AJAX发送数组副本,将数据存储在
PHP页面的数据库中.
我存储在数组中的一个数据是TinyMCE WYSIWYG编辑器的输出,因此它包含HTML,但刚刚发现这是一个问题 – 我将解释:
在我的WYSIWYG编辑器中输入一行文本并触发我的AJAX事件后,这是我的控制台中显示的JSON字符串,一切正常,数据库已发送并存储:
{"id":"229","topic":"","title":"","description":"","content":"
fgfgfdg.
","keywords":""}如果我写两行文本,这是JSON字符串并且成功:
{"id":"229","topic":"","title":"","description":"","content":"
fgfgfdg.
\nfgfgfdg
","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所说的: