我将字符串化的JSON对象发送到wordpress操作
console.log( JSON.stringify(alloptions) );
$.ajax({
type: "post",
dataType: "json",
url: ajaxurl,
processData: false,
data: {
'action': 'create_preset',
'preset': JSON.stringify(alloptions)
},
success: function( response ) {
console.log( response );
}
});
通过ajax发送之前的字符串化对象的控制台日志是这个
所以正确处理了字符串,
但另一方面它出现了斜杠
function _create_preset(){
if(!is_admin() && !isset($_POST['preset'])) return;
print_r($_POST['preset']);
}
add_action("wp_ajax_create_preset", "_create_preset");
给出
{\"get_presets\":\"eedewd\",\"site_width\":\"1400px\",\"layout_type\":...
我知道我可以使用
stripslashes( $_POST['preset'] )
要清理它,但这正是我想要避免的。我需要将JSON字符串发送到ajax之前的动作,而不是斜杠。
任何帮助表示赞赏!
和魔术引号不在
*更新和解决方案
杰西钉了它,WP导致了麻烦。
由于wp_unslash()
正在修复5.0中的问题
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/172
我把它添加到我的代码中
global $wp_version;
$new_preset_options = $_POST['preset'];
if ( version_compare( $wp_version, '5.0', '<' ) ) {
$new_preset_content = wp_unslash( $new_preset_options );
}else{
$new_preset_content = $new_preset_options ;
}
答案 0 :(得分:10)
WordPress很久以前就决定自动为所有全局输入变量添加斜杠($ _POST等)。他们通过内部<form action="http://rdio-service.herokuapp.com//search?q=" method="get" target="_top" >
Input artist: <input type="text" name="q"><br>
<button>Find songs</button>
</form>
函数传递它。删除这些斜杠的官方推荐方法是使用他们提供的$.getJSON
:
wp_slash()
**注意:它看起来像might be getting fixed in version 5.0,当您从全局输入变量请求值时,它们将为您执行wp_unslash
。