我有一个ajax调用,它从一个隐藏的输入字段中获取值
<p>
<input type="hidden" name="GreetingAll" id="GreetingAll" value="Hello Everyone!" />
<input type="submit" id="PleasePushMe" />
<div id="test-div1">
</div>
</p>
jQuery(document).ready(function() {
var GreetingAll = jQuery("#GreetingAll").val();
alert(GreetingAll);
jQuery("#PleasePushMe").click(function(){
jQuery.ajax({
type: \'POST\',
url: \'http://www.yoursitename.com/wp-admin/admin-ajax.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);
}
});
});
});
函数无法返回或执行任何操作
在函数中包括这些函数。php
function add_myjavascript(){
wp_enqueue_script( \'ajax-implementation.js\', get_bloginfo(\'template_directory\') . "/scripts/ajax-implementation.js", array( \'jquery\' ) );
}
add_action( \'init\', \'add_myjavascript\' );
function MyAjaxFunction(){
//get the data from ajax() call
$GreetingAll = $_POST[\'GreetingAll \'];
$results = "<h2>".$GreetingAll."</h2>";
// Return the String
die($results);
}
// creating Ajax call for WordPress
add_action( \'wp_ajax_nopriv_ MyAjaxFunction\', \'MyAjaxFunction\' );
add_action( \'wp_ajax_ MyAjaxFunction\', \'MyAjaxFunction\' );
你知道为什么这个代码不起作用吗?