我的问题是:无法使用jQuery执行POST请求。下面是完整的描述:
我在页面中创建了一个链接(使用帖子和页面的插件PHP代码):
<?php
    $nonce = wp_create_nonce("test_nonce");
    $link = admin_url(\'admin-ajax.php?action=my_delete_object&object_id=\'.$object_id.\'&nonce=\'.$nonce);
    echo "<a data-object_id=\\"".$object_id."\\" data-nonce=\\"".$nonce."\\" href=\\"".$link."\\" class=\\"delete-object\\" >Click Here!</a>
?>
 然后我将其添加到我的子主题函数中。php:
wp_enqueue_script( \'js_functions\', get_bloginfo( \'stylesheet_directory\' ) . \'/js/functions.js\', array( \'jquery\' ), false);
wp_localize_script( \'js_functions\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
add_action("wp_ajax_my_delete_object", "my_delete_object");
add_action("wp_ajax_nopriv_my_delete_object", "my_must_login");
function my_delete_object() {
    echo "<script> alert(\'test: ajax request reached action!\') </script>";
    // nonce check and other actions;
    $result[\'type\'] = "success";
    die(); 
}
function my_must_login() {
    //tell user to log in;
    $result[\'type\'] = "success";
    die();
}
 如果单击该链接,我会收到警报(“测试:ajax请求已达到操作!”)正确,如果我向我的\\u delete\\u对象添加其他操作,它们也会发生。
当我想向场景中添加ajax请求时,问题就出现了。
在我的子主题文件夹中,我创建了该文件夹js, 然后把文件functions.js 内装内容:
jQuery(document).ready(function($) { 
   $(".delete-object").click( function(event) {
      event.preventDefault();
      object_id = $(this).attr("data-object_id");
      nonce = $(this).attr("data-nonce");
      $.post({
         url : MyAjax.ajaxurl,
         data : {action: \'my_delete_object\', object_id: object_id, nonce: nonce},
         success: function(response) {
            if(response.type == "success") {
               alert("Ajax request returned success!");
            }
            else {
               alert("Ajax request returned something else.");
            }
         },
         error: function(){
              alert("Ajax request no good");
         }
      });   
      alert(\'click triggered action!\');
   });
 });
 当我点击链接时,它什么也不做。它只会弹出警报“单击触发的操作!”,其他什么都没有。
我的结论是,这篇文章没有发生。知道为什么吗?