using ajax to return json
-
I am not clear if this is possible or not… just testing things out at this point, plan to clean up and move to a plugin.
I have a simple php function that return results from a query
add_action( 'wp_ajax_ajax_get_geo', 'ajax_get_geo' ); add_action( 'wp_ajax_nopriv_ajax_get_geo', 'ajax_get_geo' ); // This lines it's because we are using AJAX on the FrontEnd. function ajax_get_geo(){ global $wpdb; $table_name = $wpdb->prefix . "wbmexp_geo"; $sql = "SELECT * FROM $table_name"; $results = $wpdb->get_results( $sql ); echo json_encode($results); die(); }
and than I have a script to call this via ajax
function getGeo() { jQuery.ajax({ url : '/wp-admin/admin-ajax.php', type : 'POST', dataType: "json", data : { action : 'ajax_get_geo' }, success: function(obj) { geoData = obj; console.log(geoData); } }); return geoData; }
Now if I call this from another page /script block I can see the console.log of the json, but what I would like to do is get the return json on this page and use it throughout the page/script – is this possible? I am getting ‘undefined’.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘using ajax to return json’ is closed to new replies.