Skip to:
Content
Pages
Categories
Search
Top
Bottom

got a problem retrieving a JSON object sent by ajax to php…


  • grosbouff
    Participant

    @grosbouff

    Hi ! First; WHY can’t I reply to my own post ???

    Well, this is what I came to related to this subject. The ajax process succeed (don’t know if its good but it works); but now I can’t decode de JSON object sent… :

    in the PHP admin page :

    <script type="text/javascript">
    //<![CDATA[
    var blog_url='<?php echo site_url();?>';
    //]]>
    </script>

    in the JS :

    jQuery(submit_button).click(function(){ //CLICK SUBMIT
    var dataString = jQuery.toJSON(mainlist.get_tree_array());

    jQuery.post(blog_url+'/wp-content/plugins/bp-classifieds/admin/bp-classifieds-ajax.php', {
    action: 'bp_classifieds_update_tree',
    'blog_url':blog_url,
    'cookie': encodeURIComponent(document.cookie),
    '_wpnonce': nonce,
    'datas':dataString
    },
    function(response) {
    console.log(response);
    }, "json");
    return false;
    });

    the ajax file :

    <?php
    $page_path = getcwd();
    $page_path_split = split('wp-content',$page_path);
    $wp_path = $page_path_split[0];

    include_once($wp_path.'/wp-load.php' );

    $function = $_POST['action'];
    $function();

    function bp_classifieds_update_tree() {
    global $bp,$wpdb;
    if (!check_admin_referer('classifieds-settings') ) {
    echo "NONCE error";
    return;
    }
    $res = json_decode($json, true);
    print_r($res);

    }
    add_action( 'wp_ajax_bp_classifieds_update_tree', 'bp_classifieds_update_tree' );

    ?>

    It works, I can use wpdb functions; the nonce is checked and is OK; but now I have problems with JSON :

    If I “console.log” it in my JS, I got

    {"0": {"id": "3", "order": 1}, "1": {"id": "4", "order": 2}, "2": {"id": "5", "order": 3}, "3": {"id": "6", "order": 4}, "4": {"id": "7", "order": 5}, "5": {"id": "8", "order": 6}, "6": {"id": "new", "order": 7, "deleted": true}}

    Once sent by POST in bp-classifieds-ajax.php;

    $json = $_POST['datas'];
    $res = json_decode($json, true);
    print_r($json);

    gives

    {"0": {"id": "3", "order": 1}, "1": {"id": "4", "order": 2}, "2": {"id": "5",
    "order": 3}, "3": {"id": "6", "order": 4}, "4": {"id": "7", "order": 5}, "5": {"id
    ": "8", "order": 6}, "6": {"id": "new", "order": 7, "deleted": true}}

    but

    $json = $_POST['datas'];
    $res = json_decode($json, true);
    print_r($res);

    outputs nothing. What is wrong here ?

    Thanks !

Viewing 2 replies - 1 through 2 (of 2 total)

  • grosbouff
    Participant

    @grosbouff

    PS : It’s not shown in this forum (don’t know why) but the print_r($json) returned by PHP is full of slashes in front of every

    $function = $_POST['action'];
    $function();

    function bp_classifieds_update_tree() {
    global $bp,$wpdb;
    if (!check_admin_referer('classifieds-settings') ) {
    echo "NONCE error";
    return;
    }
    $res = json_decode($json, true); //!
    print_r($res);

    }

    Where exactly is that $json being set? ;)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘got a problem retrieving a JSON object sent by ajax to php…’ is closed to new replies.
Skip to toolbar