简体   繁体   English

尝试将 FormData 中的 Blob 从 ajax(Wordpress 插件)发布到 php 时获取 400(错误请求)

[英]Getting 400 (Bad Request) when trying to post a Blob in FormData to php from ajax (Wordpress Plugin)

so please take a look at the code below.所以请看下面的代码。

        const chunk = file.slice(start,start + chunkSize + 1)
        const fd = new FormData()
        fd.append('data', chunk)
        $.ajax({
            type: 'POST',
            //cache: false,
            //contentType: false,
            //processData: false,
            url: ajax_object.ajaxurl,
            data:{
                action:'uploadChunk',
                //chunk: fd
            },
            success: function(response){
                console.log(response)
            }
        })

When I leave these comments in, the code returns with its intended response... But when I remove the comments so that I can actually send the FormData with the request, I get a 400 (bad request) error.当我留下这些评论时,代码会返回其预期的响应......但是当我删除评论以便我可以实际发送带有请求的 FormData 时,我收到 400(错误请求)错误。 All I'm doing on the backend for now is echoing back a string.我现在在后端所做的只是回显一个字符串。 That's it.而已。 And that works unless I try to send the formData along with it.除非我尝试将 formData 与它一起发送,否则这会起作用。

Any and all insight you can provide is helpful and I appreciate it greatly.您可以提供的任何和所有见解都是有帮助的,我非常感谢。 Thank you~谢谢~

When you're using a FormData object in ajax you pass that object alone to the ajax function. If you have to pass any other data use append.当您在 ajax 中使用 FormData object 时,您将 object 单独传递给 ajax function。如果您必须传递任何其他数据,请使用 append。

    const chunk = file.slice(start,start + chunkSize + 1)
    const fd = new FormData()
    fd.append('data', chunk)
    fd.append('action', 'uploadChunk')
    $.ajax({
        type: 'POST',
        //cache: false,
        contentType: false,
        processData: false,
        url: ajax_object.ajaxurl,
        data: fd,
        success: function(response){
            console.log(response)
        }
    })

pass the form data variable as data:将表单数据变量作为数据传递:

 const chunk = file.slice(start,start + chunkSize + 1)
 const fd = new FormData()
 fd.append('data', chunk)

   dataType: "json",
   data:{
         data:fd 
       },

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

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