php - Ajax request on wordpress returns 404 error -


on function.php file on custom theme folder have this:

function getprices(){     $price = get_post_meta($_request["post_id"], "price_one", true);     $results['price'] = $price;     $results = json_encode($results);     die($results); }      add_action('wp_ajax_getprices', 'getprices');     add_action('wp_ajax_nopriv_getprices', 'getprices'); 

and on js file had this:

$('ul.people_adult li').click(function(){ var post_id = $("#post_id").val();                 jquery.ajax({                     type:"post",                     datatype : "json"                     url: "http://localhost/wordpress/wp-admin/admin-ajax.php",                     data: {                         action: 'getprices',                         post_id: post_id                     },                     complete:function(data){                         console.log(data.price);                     }                     }); }); 

when click following error message: post http://localhost/wp-admin/admin-ajax.php 404 (not found)

also when alert returned data shows object object when log console shows html code of responded page.

any ideas ??

thanks!

the big problem bad url providing , reason couldn't notice until jackson , cointilt mention it.

also had typo errors in function.php file.

here answer problem.

function getprices(){     $the_id = $_request["post_id"];     $results[] = get_post_meta($the_id, "price_one", true);     $results[] = get_post_meta($the_id, "price_two", true);     $results[] = get_post_meta($the_id, "price_three", true);     $results[] = get_post_meta($the_id, "price_four", true);     $results[] = get_post_meta($the_id, "price_five", true);     $results[] = get_post_meta($the_id, "price_six", true);     $results[] = get_post_meta($the_id, "price_seven", true);     $results[] = get_post_meta($the_id, "price_eight", true);     $results = json_encode($results);     die($results); } add_action('wp_ajax_getprices', 'getprices'); add_action('wp_ajax_nopriv_getprices', 'getprices'); 

and on js file

$('ul.people_adult li').click(function(){     var post_id = $("#post_id").val();                         jquery.ajax({                             type:"post",                             datatype : "json",                             url: "http://localhost/wordpress/wp-admin/admin-ajax.php",                             data: {                                 action: 'getprices',                                 post_id: post_id                             },                             success:function(response){                                 $("#price_est_person").html(response[0]); /*do stuff response[1], response[2 etc]*/                             }                             });     }); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -