Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop jQuery ajax from adding slashes to JSON string?

I am sending stringified JSON object to a wordpress action

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 );

    }
});

the console log of the stringified object before sent via ajax is this

http://prntscr.com/7990ro

so the string is correctly processed ,

but on the other side it comes out with slashes

function _create_preset(){

    if(!is_admin() && !isset($_POST['preset'])) return;

    print_r($_POST['preset']);

}

add_action("wp_ajax_create_preset", "_create_preset");

gives

{\"get_presets\":\"eedewd\",\"site_width\":\"1400px\",\"layout_type\":...

I know I can use

stripslashes( $_POST['preset'] )

to clean it up but that is what I am trying to avoid. I need the JSON string to be sent to the action as it is before the ajax, without slashes.

any help is appreciated!

and magic quotes is not on

http://prntscr.com/7996a9

*UPDATE AND SOLUTION

Jesse nailed it and WP was causing the trouble. Since the wp_unslash() is on its way to fix this in 5.0 https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/172 I added this to my code

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 ;
}
like image 492
Benn Avatar asked May 25 '15 16:05

Benn


People also ask

Why does my JSON have slashes?

Those backslashes are escape characters. They are escaping the special characters inside of the string associated with JSON response. You have to use JSON. parse to parse that JSON string into a JSON object.

Can AJAX be used with JSON?

According to the AJAX model, web applications can send and retrieve data from a server asynchronously without interfering with the display and the behavior of the existing page. Many developers use JSON to pass AJAX updates between the client and the server.

What is JSON Stringify in AJAX call?

JSON. stringify() calls toJSON with one parameter, the key , which has the same semantic as the key parameter of the replacer function: if this object is a property value, the property name. if it is in an array, the index in the array, as a string. if JSON.


1 Answers

WordPress made the decision a long time ago to auto add slashes to all global input variables ($_POST, etc). They pass it through an internal wp_slash() function. The official recommended way to remove these slashes is to use their provided wp_unslash:

wp_unslash( $_POST['preset'] );

Here is the codex reference.

**Note: It looks like this might be getting fixed in version 5.0, where they will be doing the wp_unslash for you when you request values from the global input variables.

like image 170
Jesse Kernaghan Avatar answered Oct 24 '22 18:10

Jesse Kernaghan