在为这个问题找到解决方案之后,我没有得到我犯的错误。如果你不能回答,请为我的问题提出你宝贵的答案,并提出不要阻止的谦卑请求。
所以我在WordPress中尝试Ajax Call并遇到问题"致命错误:在C:\ xampp \ htdocs \ brandcart \ wp-content \ themes \ twentyten \ functions.php中调用未定义的函数add_action()第51行"
WordPress:4.1.1 主题:二十个
这是我的代码:
in Twenty-ten page.php
<input type="hidden" name="GreetingAll" id="GreetingAll" value="Hello Everyone!" />
<input type="submit" id="PleasePushMe" />
<div id="test-div1">
</div>
in Twenty-ten functions.php
add_action( 'init', 'add_myjavascript' );
function add_myjavascript(){
wp_enqueue_script( 'ajax-implementation.js', get_bloginfo('template_directory') . '/script/ajax-implementation.js', array( 'jquery' ) );
}
// creating Ajax call for WordPress
add_action( 'wp_ajax_nopriv_MyAjaxFunction', 'MyAjaxFunction22' );
add_action( 'wp_ajax_MyAjaxFunction', 'MyAjaxFunction22' );
function MyAjaxFunction22(){
//get the data from ajax() call
$GreetingAll = $_POST['GreetingAll '];
$results = "<h2>".$GreetingAll."</h2>";
// Return the String
die($results);
}
和我的ajax-implementation.js(在主题的脚本目录中)
jQuery(document).ready(function() {
var GreetingAll = jQuery("#GreetingAll").val();
jQuery("#PleasePushMe").click(function(){ jQuery.ajax({
type: 'POST',
url: 'http://localhost/brandcart/wp-content/themes/twentyten/functions.php',
data: {
action: 'MyAjaxFunction',
GreetingAll: GreetingAll,
},
success: function(data, textStatus, XMLHttpRequest){
jQuery("#test-div1").html('');
jQuery("#test-div1").append(data);
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
});
});
请帮助我,我错了。