php message Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

前端 未结 2 900
渐次进展
渐次进展 2020-12-20 12:47

This is my javascript

function ajax_post(){
            // Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
            // Create          


        
相关标签:
2条回答
  • 2020-12-20 13:23

    This works as well. Taken from https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type

    const config = { headers: { Authorization: Bearer ${localStorage.token}, "Content-Type": "multipart/form-data; boundary=---------------------------974767299852498929531610575" } };

    0 讨论(0)
  • 2020-12-20 13:35

    The browser sets the correct headers (including the correct multipart boundary indication in the Content-type) if you haven't manually specified anything.

    So all you need to do is remove the following line:

     hr.setRequestHeader("Content-type", "multipart/form-data");
    

    Otherwise you will need to set the boundary yourself as it is suggested by Ellias Van Ootegem in Uploading a file with XMLHttprequest - Missing boundary in multipart/form-data:

    hr.setRequestHeader("Content-type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2));
    
    0 讨论(0)
提交回复
热议问题