douqiao5552 2018-07-10 20:31
浏览 135

在WordPress中通过AJAX发布上传文件

Aware there are a few questions around this topic, I have been searching through them most of this evening trying to find a solution to my problem. This is the first time I have worked on such task.

I am trying to upload a file via AJAX in WordPress. I have set up AJAX in WordPress and the connection works fine, until I try to pass the form data at which stage I am getting a 'bad request' error and no other information other than this.

The AJAX callback function in WordPress is simple enough:

add_action( 'wp_ajax_nopriv_calendar_process', 'calendar_process' );
add_action( 'wp_ajax_calendar_process', 'calendar_process' );

function calendar_process() {
    echo "worked";
    die();
}

My front end form looks like this:

<form method="post" class="custom-cal-form">
        <input type="file" accept=".ics" name="custom-calendar" id="custom_cal_file" />

        <button class="submit-ajax">Update Calendars</button>
    </form>

And my AJAX call looks like this:

jQuery( document ).on( 'click', '.submit-ajax', function(e) {

        e.preventDefault();

        var file_data = jQuery('#custom_cal_file').prop('files')[0];
        var form_data = new FormData();
        form_data.append('file', file_data);
        console.log(form_data);

        jQuery.ajax({

            url : calendarprocess.ajax_url,
            type : 'post',
            processData: false,
            contentType: false,
            data : {
                action : 'calendar_process',
                post_id : 'test',
                calendar : form_data
            },
            success : function( response ) {
                jQuery('.ajax-response').html( response );
            }
        });

    });

I was originally getting the error

Can only call FormData.append on instances of FormData

Until I looked around at some questions and added

processData: false, contentType: false,

To my form and now the full error I get is:

Failed to load resource: the server responded with a status of 400 (Bad Request)

Which of course signals that WordPress is not happy.

Really hoping someone can add some insight into this? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duandi4815 2018-07-10 20:46
    关注

    Not familiar with WordPress, but sounds like your URL is bad. If you're trying to concatenate those two "variables?" together, "." is not valid in Javascript. You're looking for "+". Not seeing where those are being initiated at, so if that's something WP specific, ignore that, lol.

    First thing I'd do to debug is hard code the address to the page you're calling first. I.E. "test/test.php". That would at least tell you if it's a valid address.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端