简体   繁体   English

如何在Wordpress中使用AJAX正确发送FormData?

[英]How to properly send FormData using AJAX in Wordpress?

I've been reading a lot of questions on Stack Overflow and implementing the answers. 我一直在阅读有关Stack Overflow的许多问题并实现答案。 Nothing seems to work, tough. 似乎没有任何效果,很难。 I want to allow users to upload files and I've managed to implement it without AJAX. 我希望允许用户上传文件,并且设法在没有AJAX的情况下实现了文件。 It was all in one PHP script(PHP, Javascript, CSS). 它全都在一个PHP脚本(PHP,Javascript,CSS)中。 While seperating everything to clean it up, I decided to try and make it use AJAX. 在分离所有东西进行清理的同时,我决定尝试使其使用AJAX。

Here's the link to my javascript file: AJAX 这是我的JavaScript文件的链接: AJAX

The file contains an AJAX request for deletion, as well. 该文件还包含一个AJAX删除请求。 But, I'm not worrying about if that works right now. 但是,我不担心现在是否可行。

And here's the link to upload.php: upload.php 这是upload.php的链接: upload.php

This PHP file is not called directly, I'm hooking it int an action hook, like so: 这个PHP文件没有被直接调用,我将其钩接到一个动作钩子中,如下所示:

add_action( 'wp_ajax_vibesoft_files_upload', 'vibesoft_files_upload' );
function vibesoft_files_upload() {
    include 'upload.php';
}

This code is then included in the functions.php of my active theme. 然后,此代码包含在我的活动主题的functions.php中。

EDIT: Forgot to include what gets printed to the console: 编辑:忘记包括要打印到控制台的内容: 在此处输入图片说明

EDIT 2: Found a possible clue! 编辑2:找到了可能的线索! Yay! 好极了! Here's a snippet of code from admin-ajax.php: 这是来自admin-ajax.php的代码片段:

// Require an action parameter
if ( empty( $_REQUEST['action'] ) )
    wp_die( '0', 400 );

Since I got back a 400 in the console log, I changed the 400 here to 666. The success function ran and it failed this check from my AJAX script: 由于我在控制台日志中获得了400,因此将此处的400更改为666。成功函数运行了,并且它未能通过我的AJAX脚本进行此检查:

if ( typeof data == 'object' ) {
    console.log( 'data is JSON' );
}

On the next line, there is: 在下一行中,有:

console.log( data )

Which prints a single 0 and admin-ajax has a wp_die( '0' ) at the end of it. 它显示单个0,并且admin-ajax在其末尾带有wp_die('0')。

EDIT 3: So, I've changed the $.ajax invocation a bit: 编辑3:因此,我对$ .ajax调用进行了一些更改:

$( '#user-file' ).change( function( event ) {
    var formData = new FormData();
    formData.append( "action", "vibesoft_files_upload" );
    formData.append( "_wpnonce", vbsoft.nonce_upload );
    formData.append( "user-file", $(this)[0].files[0] );

$.ajax({
    type: "POST",
    url: vbsoft.ajax_url,
    data: formData,
    contentType: false,
    processData: false,
    dataType: 'JSON',

Of course, this is not the complete function. 当然,这不是完整的功能。 What I've changed is how I prepare FormData and the new thing that's happening is: If I remove the dataType: 'JSON' from the code, I get a complete page in response(I don't know which one, there's a LOT of html and stuff I get back). 我更改的是如何准备FormData,发生的新事情是:如果我从代码中删除了dataType:'JSON',则会得到完整的响应页面(我不知道哪一个,有很多) html和我回来的东西)。 If I keep data: 'JSON' there, I get an Object with statusText:"parseerror" and responseText:"the_entire_page_here" 如果我在那里保存数据:“ JSON”,则会得到一个带有statusText:“ parseerror”和responseText:“ the_entire_page_here”的对象

The first line, after 第一行,之后

wp_send_json( array( 'error' => 'Test' ) );

How come I'm not getting JSON back? 为什么我不找回JSON?

EDIT 4: 编辑4:

Okay, tried changing all of the $ to jQuery - still no change. 好的,尝试将所有$更改为jQuery-仍然没有更改。 I have modified my admin-ajax.php a little, for debugging purposes: 为了进行调试,我对admin-ajax.php做了一些修改:

// Require an action parameter
if ( empty( $_REQUEST['action'] ) ) {
    wp_send_json( array( 'error' => 'no action parameter' ) );
    wp_die( '0', 400 );
}

Now, I'm getting JSON back. 现在,我得到了JSON。 Sweet! 甜! So, I'm somehow not sending an action parameter in my data. 因此,我不知何故没有在数据中发送动作参数。 I don't get it. 我不明白 I've seen several examples online which use formData.append( "action", "action_name_without_wp_ajax" ); 我在网上看到了几个使用formData.append(“ action”,“ action_name_without_wp_ajax”)的示例; in various places. 在各个地方。

EDIT 5: 编辑5:

I've been looking at the Network tab of the Chrome Dev tools a bit. 我一直在浏览Chrome开发工具的“网络”标签。 contentType:false

That's what I get in the response header if I set contentType: false, and if I set it to contentType: "multipart/form-data", I get: 如果我将contentType设置为false,那么我将在响应标头中得到该结果;如果将其设置为contentType:“ multipart / form-data”,则会得到:

contentType:“多部分/表单数据”

请考虑将您的所有“ $”更改为“ jQuery”

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

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