got a problem retrieving a JSON object sent by ajax to php…
-
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 !
- The topic ‘got a problem retrieving a JSON object sent by ajax to php…’ is closed to new replies.