简体   繁体   English

Wordpress admin-ajax.php 400(错误请求)错误

[英]Wordpress admin-ajax.php 400 (Bad Request) error

I am trying to call a php function with a button press.我正在尝试通过按下按钮呼叫 php function。

This is my code so far:到目前为止,这是我的代码:

    echo("<button onclick=\"import_file()\"> Import </button>");        

The Javascript upload.js file: Javascript upload.js文件:

    function import_file () {
   
     jQuery.ajax({
        url: my_ajax_object.ajax_url, 
        data: {action: 'final_save'},
        type: "POST", //request type
        success:function(result){
         alert(result);
       }
     });
 }

and the action in the primary php file:以及主要 php 文件中的操作:

 function final_save(){
    check_admin_referer('import-upload');
    set_time_limit(0);
    $this->file = $file;
    $result = $this->save();
    if ( is_wp_error( $result ) ){
        echo $result->get_error_message();
    }else{
        echo "Success";
    }

}

add_action('wp_ajax_final_save', 'final_save');
add_action('wp_ajax_no_priv_final_save', 'final_save');

I also enqueued the scripts in the same file:我还将脚本排入同一个文件中:

function enqueue_script(){
    $plugins_url = plugins_url();
    $base_url = get_option( 'https://www.virtualmin.archives.com' );
    $plugins_dir = str_replace( $base_url, ABSPATH, $plugins_url );
    $plugin = $plugins_dir.'/really-simple-csv-importer/upload.js';
    wp_enqueue_script('js-file', $plugin, array('jquery'), '', false);
    wp_localize_script( 'js-file', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

    //Load jQuery
    wp_enqueue_script('jquery');

}

and calling this function from dispatch:并从调度中调用此 function:

    function dispatch() {
        $this->header();
        $this->enqueue_script();
//more code
}

Dispatch being called here:在这里调用调度:

    function really_simple_csv_importer() {
    load_plugin_textdomain( 'really-simple-csv-importer', false, dirname( plugin_basename(__FILE__) ) . '/languages' );
    
    $rs_csv_importer = new RS_CSV_Importer();
    register_importer('csv', __('CSV', 'really-simple-csv-importer'), __('Import posts, categories, tags, custom fields from simple csv file.', 'really-simple-csv-importer'), array ($rs_csv_importer, 'dispatch'));
}
add_action( 'plugins_loaded', 'really_simple_csv_importer' );

I am getting an admin-ajax.php 400 (Bad Request) error.我收到 admin-ajax.php 400(错误请求)错误。 Any idea what am I doing wrong?知道我做错了什么吗? This is being coded in a plugin.这是在插件中编码的。

I seem to remember reading that you need to register, localize and then enqueue the script when using ajax so instead of我似乎记得读过你需要在使用 ajax 时注册,本地化然后排队脚本而不是

wp_enqueue_script('js-file', $plugin, array('jquery'), '', false);
wp_localize_script( 'js-file', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

try尝试

wp_register_script('js-file', $plugin, array('jquery'), '', false);
wp_localize_script( 'js-file', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script('js-file', $plugin, array('jquery'), '', false);

I hope this helps我希望这有帮助

Also I know this is probably not the problem, but I thought you needed to have wp_die() or die;我也知道这可能不是问题,但我认为你需要有 wp_die() 或死; always in the function being called ie final_save总是在function被调用即final_save

I can see that there is typo error in your php file so just replace following line我可以看到您的 php 文件中存在拼写错误,因此只需替换以下行

add_action('wp_ajax_no_priv_final_save', 'final_save');

with following one与以下一个

add_action('wp_ajax_nopriv_final_save', 'final_save');

Hope this help to solve error.希望这有助于解决错误。

Besides the answers already given by @jtowell, you're gonna want to add a nonce to your localization and check the referer on the backend.除了@jtowell 已经给出的答案之外,您还需要在本地化中添加一个随机数并检查后端的引荐来源网址。 As this is might be confusing the first time around I highly recommend following the steps in the documentation.由于第一次使用时可能会造成混淆,我强烈建议您按照文档中的步骤进行操作。

General walkthrough: https://developer.wordpress.org/plugins/javascript/ajax/一般演练: https://developer.wordpress.org/plugins/javascript/ajax/

Specific docs: https://developer.wordpress.org/reference/functions/check_ajax_referer/具体文档: https://developer.wordpress.org/reference/functions/check_ajax_referer/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM