php – 如何阻止jQuery ajax添加斜杠到JSON字符串?
|
我将字符串化的 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发送之前,字符串化对象的控制台日志就是这个 http://prntscr.com/7990ro 所以字符串被正确处理, 但在另一方面,它出现了斜线 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之前的动作,而不是斜杠. 任何帮助表示赞赏! 和魔法引号没有 http://prntscr.com/7996a9 *更新和解决方案 杰西钉了它,WP引起了麻烦. 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 ;
}
很久以前,WordPress决定自动为所有全局输入变量添加斜杠($_POST等).它们通过内部wp_slash()函数传递它.删除这些斜杠的官方推荐方法是使用他们提供的wp_unslash:
wp_unslash( $_POST['preset'] ); Here is the codex reference. **注意:它看起来像这个might be getting fixed in version 5.0,当您从全局输入变量请求值时,它们将为您执行wp_unslash. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
