【问题标题】:WordPress AJAX request keep saying "The requested URL was not found on this server."WordPress AJAX 请求一直说“在此服务器上找不到请求的 URL。”
【发布时间】:2021-07-23 12:45:58
【问题描述】:

我使用此线程 Add custom my account menu item based on user role in WooCommerce 3+ 在 WooCommerce 菜单中添加了一个自定义链接,它的工作正常,符合我的预期。

现在我有 select2 库,并且在我的新自定义页面 http://localhost/wordpress/my-account/clients/ 我添加了一个能够选择多个的下拉列表。

下面是我的代码,通过它我可以检测用户是否从下拉列表中删除了一个项目,然后我会触发以下事件。

jQuery('.clientchildren').on('select2:unselect', function (evt) {  
  
  var userId = evt.params.data.id;     
  
  jQuery.ajax({
              type: "POST",
              url: '/wp-admin/admin-ajax.php',  
              dataType: 'json',
              delay: 250, // delay in ms while typing when to perform a AJAX search
              data : {
                        action : 'remove_client_children_user_id',
                        userId : userId,                        
              },                       
              success: function(data)
              {  
                var data = JSON.parse(data);      
                toastr["success"]("Removed Successfully." );          
                
              }
            });

});

在我的 functions.php 文件中,我放了以下代码。

functions.php

add_action( 'wp_ajax_nopriv_remove_client_children_user_id', 'remove_client_children_user_id' );
add_action( 'wp_ajax_remove_client_children_user_id', 'remove_client_children_user_id' );

function remove_client_children_user_id() {
    

    echo json_encode(array('status'=>1),true);
    exit;
}

但它从不进去,一直在说

在此服务器上找不到请求的 URL。

我什至通过将action 更改为get_mindesk_var_clients 来尝试通过将action 更改为我的一些旧现有函数(例如wp_ajax_get_mindesk_var_clients)来使用ajax 调用,但它也从来没有进入过这个内部。

这是因为我创建了一个新的自定义页面并尝试实现对它的 ajax 调用吗?

有人可以指导我该怎么做才能使其正常工作,以及调用 ajax 到底在哪里出现问题?

谢谢

【问题讨论】:

  • 你把你的js代码放在哪里了?
  • 在我的一个 custom.js 文件中..它执行得很好..
  • 它进入 custom.js 文件.. 我能够传递请求,但它只是不去调用 ajax。
  • 我附上了截图..
  • 你检查过ajax URL叫什么吗?

标签: wordpress woocommerce


【解决方案1】:

您可以使用wp_localize_script 将ajax URL 传递给js。检查下面的代码。

function theme_enqueue_scripts() {
    wp_register_script( 'custom', get_stylesheet_directory_uri() . '/assets/js.custom.js', array('jquery'), null, true );
    
    wp_localize_script( 'custom', 'custom_object',
        array( 
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
        )
    );

    wp_enqueue_script( 'custom' );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );

jQuery('.clientchildren').on('select2:unselect', function (evt) {  
    var userId = evt.params.data.id;     
    jQuery.ajax({
        type: "POST",
        url: custom_object.ajaxurl,
        dataType: 'json',
        delay: 250, // delay in ms while typing when to perform a AJAX search
        data : {
            action : 'remove_client_children_user_id',
            userId : userId,                        
        },                       
        success: function(data){  
            var data = JSON.parse(data);      
            toastr["success"]("Removed Successfully." );          
        }
    });
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 2018-05-08
  • 2017-03-21
  • 2010-10-16
  • 1970-01-01
相关资源
最近更新 更多