简体   繁体   English

WordPress Ajax 400 错误请求

[英]WordPress Ajax 400 Bad Request

I have been through 21 different posts on this site and many others on google, currently on page 13 and i'm actually about to cry.我已经在这个网站上浏览了 21 个不同的帖子,在谷歌上浏览了许多其他帖子,目前在第 13 页,我真的要哭了。 I am a junior developer and I am really struggling to get this to work.我是一名初级开发人员,我真的很难让它发挥作用。 For some reason I can't seem to grasp it.出于某种原因,我似乎无法理解它。

I am trying to create a plugin that will allow me to upload a blob file to WordPress media library and it's failing miserably.我正在尝试创建一个插件,允许我将 blob 文件上传到 WordPress 媒体库,但它失败了。

The plugin structure is like so:插件结构是这样的:

ajax_test/
-- ajax_test.php

-- assets/
-- -- js/
-- -- -- uploader.js

In a file called uploader.js I have this code:在名为 uploader.js 的文件中,我有以下代码:

mediaRecorder.addEventListener('stop', function() {
    audio.src = URL.createObjectURL(new Blob(recordedChunks));
    downloadLink.href = URL.createObjectURL(new Blob(recordedChunks));
    downloadLink.download = 'file' + Date.now() + '.wav';
    downloadLink.classList.remove('hide');

    var blob = new Blob(recordedChunks);
    var fd = new FormData();
    fd.append('fname', 'test.wav');
    //fd.append('data', event.target.result);
    fd.append('file', blob);
    fd.append('action', 'send_message');

    console.log('about to ajax');
    
    $.ajax({
            url: ibenicUploader.ajax_url,
            type: 'POST',
            data: fd,
            cache: false,
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            success: function(data, textStatus, jqXHR) {  
    
                if( data.response == "SUCCESS" ){
                
                    console.log('success');
                
                } else {
                    console.log('error');
                }
            }
        });

        console.log('after ajax');

});

And in the ajax_test.php file I have this code:在 ajax_test.php 文件中,我有以下代码:

function ibenic_enqueue() {
wp_enqueue_script( 'ibenic-uploader', plugins_url( 'assets/js/uploader.js', __FILE__  ), array('jquery'), '1.0', true );
wp_localize_script( 'ibenic-uploader', 'ibenicUploader',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'ibenic_enqueue' );

In the console I get在控制台我得到

about to ajax即将到阿贾克斯

after the ajax在阿贾克斯之后

/wp-admin/admin-ajax.php 400 (Bad Request) /wp-admin/admin-ajax.php 400(错误请求)

Even if I change ibenicUploader.ajax_url, to a full path I get the same error.即使我将ibenicUploader.ajax_url,更改为完整路径,我ibenicUploader.ajax_url,遇到相同的错误。

Please can someone help me upload this file to wordpress请有人帮我上传这个文件到wordpress

You will have to logged in to it work as wp_ajax works with logged in user.您必须登录它才能工作,因为 wp_ajax 与登录用户一起工作。

add_action("wp_ajax_your_fn", "your_fn");

For visitor use wp_ajax_nopriv对于访客使用 wp_ajax_nopriv

 add_action("wp_ajax_nopriv_your_fn", "your_fn");

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

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