简体   繁体   中英

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. 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.

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.

    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 
       },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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