Javascript 在ajax php文件上无法识别Wordpress内置函数
我是Wordpress开发的新手,我在Wordpress上很难使用ajax 我正在尝试构建一个使用ajax的插件。我的php文件Javascript 在ajax php文件上无法识别Wordpress内置函数,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我是Wordpress开发的新手,我在Wordpress上很难使用ajax 我正在尝试构建一个使用ajax的插件。我的php文件(xxxecommerce.ajax.php)无法识别wordpress上的内置函数,比如get\u选项('xxx-ecommerce-admin2-email')我甚至无法扩展到WP_小部件(老实说,我不确定是否需要扩展,我只是尝试过) 这就是我如何包含文件的方式 wp_enqueue_script( 'xxxecommerce1', plugins_
(xxxecommerce.ajax.php)
无法识别wordpress上的内置函数,比如get\u选项('xxx-ecommerce-admin2-email')代码>我甚至无法扩展到WP_小部件(老实说,我不确定是否需要扩展,我只是尝试过)
这就是我如何包含文件的方式
wp_enqueue_script(
'xxxecommerce1',
plugins_url( '/script.js', __FILE__ ),
array( 'jquery' )
);
wp_localize_script(
'xxxecommerce1',
'the_ajax_script',
array( 'ajaxurl' => plugins_url( '/xxxecommerce.ajax.php', __FILE__ ) )
);
这是我的script.js
jQuery(document).ready(function() {
jQuery('.xxx-ecommerce-prod-opt').click(function() {
var data = {
action: 'test_response',
post_var: 'this will be echoed back'
};
jQuery.post(the_ajax_script.ajaxurl, data, function(response) {
alert(response);
});
});
});
这是我的xxxecommerce.ajax.php
$x = new xxxecommerceAjax();
$x->getEmail();
class xxxecommerceAjax extends WP_Widget
{
function getEmail() {
$email = get_option('xxx-ecommerce-admin2-email');
echo $email;
}
}
我希望有人能帮我。提前感谢您不能像这样调用ajax,您必须通过ajax url调用admin-ajax.php文件,并定义一个操作看看这个示例:
$.ajax({
url: 'admin-ajax.php',
type: 'post',
data: {action: 'my_action'},
dataType: 'json'
});
请注意,我定义了一个操作来向admin-ajax.php文件发送请求。现在在wordpress中有两个内置钩子来处理您的ajax请求
wp\u ajax\u您的\u ajax\u操作-wp\u ajax\u我的操作
wp\u ajax\u nopriv\u你的\u ajax\u动作-wp\u ajax\u nopriv\u我的动作
你可以这样称呼它:
// Call it in your themes functions.php file or in your plugin
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
// Access more data through ajax request
// any php method here to send the response
// include your xxxecommerce.ajax.php file here and send response
die();
}
看看这个例子,它将对您有更多的帮助,并且对wp\u ajax\uuu
和wp\u ajax\u nopriv\uu
做更多的研究
希望这对你有帮助;)
新年快乐。非常感谢!你帮了大忙。