我正在一个Wordpress项目中工作,我想在我的网站上通过Ajax加载一些东西。我正在通过ajax加载许多节,但之前从未遇到过错误。同样,在这里,我正在做应该怎么做的所有事情,但是我不知道为什么会收到400错误的请求错误。这是代码:
if(!function_exists('enque_this')){
function enque_this(){
wp_enqueue_style( 'style-name', MY_PLUGIN_LOCATION.'css/style.css');
wp_enqueue_script('my_script_file',MY_PLUGIN_LOCATION.'js/md-referral.js','jQuery','',true);
wp_localize_script('my_script_file','ajax_object',array('ajax_url' => admin_url('admin-ajax.php'),'we_value'=>1234));
}}
add_action('wp_enqueue_scripts','enque_this');
if(!function_exists('enque_single_prod_page')){
function enque_single_prod_page(){
wp_enqueue_script('my_script_file_2nd',MY_PLUGIN_LOCATION.'js/md-single.js','jQuery','',true);
}}
add_action('wp_enqueue_scripts','enque_single_prod_page');
function how_much_dis(){
echo 'really .. . .!!! i Got SomeTHing ??';
};
add_action('wp_ajax_how_much_dis','how_much_dis');
jQuery代码
jQuery(document).on("click","#submit_reff_code",function(){
var codes = jQuery("#input_reff_code").val();
var button_text = jQuery("#submit_reff_code").val("Please Wait . . .");
var is_disabled = jQuery('[name="add-to-cart"]').prop('disabled');
var product_id = jQuery('[name="add-to-cart"]').val();
var variation_id = jQuery('[name="variation_id"]').val();
if( codes != "" && codes.length > 2 && is_disabled == false && jQuery.isNumeric(product_id)){
var data = {
'action' : 'how_much_dis',
'product_id_from_prod' : product_id,
'variation_id_from_prod': variation_id,
'code' : codes
};
jQuery.post(ajax_object.ajax_url, data , function(response){
console.log(response);
});
}else{
alert("please enter valid code");
};
});
谢谢您的帮助,thanxx
400表示操作错误。问题中的代码应在用户登录时起作用。要使其在用户未登录时起作用,您必须添加以下行
add_action('wp_ajax_nopriv_how_much_dis','how_much_dis');
也许这就是原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句