JavaScript对象->;AJAX->;wp_选项->;回音对象?
我一直在尝试使用各种方法来解决这个问题,比如JavaScript对象->;AJAX->;wp_选项->;回音对象?,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我一直在尝试使用各种方法来解决这个问题,比如JSON.stringify,JSON.parse,JSON\u encode(),JSON\u decode(),stripslashes(),查找并替换,等等 但我无法确定在何处以及是否对JS对象进行编码以使其从数据库中完整返回 以下是我目前正在做的事情: 1。通过ajax将JS对象发送到WP插件: var data = { action : 'store_object', obj: myObject }; $.post(ajaxu
JSON.stringify
,JSON.parse
,JSON\u encode()
,JSON\u decode()
,stripslashes()
,查找并替换
,等等
但我无法确定在何处以及是否对JS对象进行编码以使其从数据库中完整返回
以下是我目前正在做的事情:
1。通过ajax将JS对象发送到WP插件:
var data = {
action : 'store_object',
obj: myObject
};
$.post(ajaxurl, data).done(function(res) {
console.log(res);
});
$myObject = $_POST['obj'];
$options = $this->get_admin_options();
$options['settings'] = $myObject;
update_option($this->admin_options_name, $options);
$options = $this->get_admin_options();
?>
<script>
;(function ($, window, document, undefined ) {
$(document).ready(function() {
$('#el').pluginName(<?php echo stripslashes(json_encode($options['settings'])); ?>);
});
})(jQuery, window, document);
</script>
2。将对象保存在WP插件的选项中:
var data = {
action : 'store_object',
obj: myObject
};
$.post(ajaxurl, data).done(function(res) {
console.log(res);
});
$myObject = $_POST['obj'];
$options = $this->get_admin_options();
$options['settings'] = $myObject;
update_option($this->admin_options_name, $options);
$options = $this->get_admin_options();
?>
<script>
;(function ($, window, document, undefined ) {
$(document).ready(function() {
$('#el').pluginName(<?php echo stripslashes(json_encode($options['settings'])); ?>);
});
})(jQuery, window, document);
</script>
3。将保存的对象打印为JavaScript对象:
var data = {
action : 'store_object',
obj: myObject
};
$.post(ajaxurl, data).done(function(res) {
console.log(res);
});
$myObject = $_POST['obj'];
$options = $this->get_admin_options();
$options['settings'] = $myObject;
update_option($this->admin_options_name, $options);
$options = $this->get_admin_options();
?>
<script>
;(function ($, window, document, undefined ) {
$(document).ready(function() {
$('#el').pluginName(<?php echo stripslashes(json_encode($options['settings'])); ?>);
});
})(jQuery, window, document);
</script>
$options=$this->get_admin_options();
?>
;(函数($,窗口,文档,未定义){
$(文档).ready(函数(){
$('#el').pluginName();
});
})(jQuery、窗口、文档);
myObject的内容可能包含HTML代码或特殊字符,这就是问题的根源。我想不出怎么把所有的角落都盖上。例如,上面的代码将转换为:
<a href="http://google.com">Link</a>
Hello!
This is a new line.
你好
这是一条新路线。
为此:
<a href="http://google.com">Link</a>
Hello!n
This is a new line.n
你好N
这是一条新路线
HTML正确呈现,但符号\n不正确
那么,这样做最安全的方法是什么
JS Object->ajax->wp database->将对象作为JS对象回显
$myObject=$\u POST['obj']代码>是的,谢谢。这有帮助吗:$myObject=$\u POST['obj']代码>是的,谢谢。这有帮助吗: