簡體   English   中英

如何正確設置 jQuery ajax 以上傳 Wordpress 中的文件?

[英]How to properly set up jQuery ajax to upload files in Wordpress?

我希望用戶能夠從我的自定義編輯器塊中使用 ajax 上傳 html 文件。 Wordpress 需要所有 ajax go 到 admin-ajax.ZE1BFD762321E409CEE4AC0B6E84。 我的 js 代碼在 Wordpress 中注冊和排隊的外部文件中。 這是來自https://codex.wordpress.org/AJAX_in_Plugins的 Wordpress Codex 指令:

單獨的 JavaScript 文件

與上一個示例相同,除了 JavaScript 位於單獨的外部文件中,我們將調用 js/my_query.js。 這些示例與插件文件夾相關。

jQuery(document).ready(function($) {
    var data = {
        'action': 'my_action',
        'whatever': ajax_object.we_value      // We pass php values differently!
    };
    // We can also pass the url value separately from ajaxurl for front end AJAX implementations
    jQuery.post(ajax_object.ajax_url, data, function(response) {
        alert('Got this from the server: ' + response);
    });
});

對於外部 JavaScript 文件,我們必須首先 wp_enqueue_script() 以便將它們包含在頁面中。 Additionally, we must use wp_localize_script() to pass values into JavaScript object properties, since PHP cannot directly echo values into our JavaScript file. 處理程序 function 與前面的示例相同。

<?php
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
    if( 'index.php' != $hook ) {
    // Only applies to dashboard panel
    return;
    }
        
    wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );

    // in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

這是我的 PHP:

if (isset ( $_POST["test"] ) ){
    echo 'test working';
}
if ( isset ( $_FILES["renee_wip_upload"] ) ){
    echo 'test2';
}

這是我的 js function:

function fileSubmit(e) {
    e.preventDefault();
    if(typeof(files2) !== "undefined"){
    //ajax using post() works fine which means the php side is OK
        renee_wip_ajax_object.braft_wip_upload_value = 'test';
        var data2 = {
            'action': 'renee_wip_block_ajax',
            'test': renee_wip_ajax_object.braft_wip_upload_value
        };
        jQuery.post(renee_wip_ajax_object.ajax_url, data2, function(response) {
            console.log('response test: ', response);
        }).fail(function(response) {
            console.log('Error: ' + response.responseText);
        });

        //It seems $_FILES never gets populated
        data = new FormData();
        data.append('action', 'renee_wip_block_ajax');
        for (var i = 0; i < files2.length; i++) {
            var file = files2[i];
            data.append('renee_wip_upload[]', file, file.name);
        }
        jQuery.ajax({
            success: function(data){
                console.log('data: ', data);
            },
            error: function(err){
            throw err;
            },
            type: "post",
            url: renee_wip_ajax_object.ajax_url,
            data: data,
            dataType: "html",
            enctype: 'multipart/form-data',
            processData: false,
            contentType: false,
        }).done(function(msg){
            console.log('done: ', msg);
        }).fail(function(err){
          throw err;
        });
    }
}

伙計,我真是瞎了眼。 它是: data.append('braft_wip_upload[]', file, file.name); 但應該是: data.append('renee_wip_upload[]', file, file.name); 我什至糾正了編輯中的錯誤,但忘了在我的代碼中這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM