Send XMLHttpRequest with both Header and FormData

后端 未结 1 1296
野的像风
野的像风 2021-01-22 12:48

I am trying to send a XMLHttpRequest with a header and add a FormData. Is there an (elegant) way i can do something like this:

var formData = new FormData();
for         


        
相关标签:
1条回答
  • 2021-01-22 13:45

    You cannot specify the Content-Type header when sending FormData because that header automatically gets set to "multipart/form-data" by the browser. You can set other headers though, try this:

    var formData = new FormData();
    formData.append("file", file);
    formData.append("mod", "fileupload");
    formData.append("token", "add");
    
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/ajax_gateway.php");
    xhr.setRequestHeader("X-Answer", "42");
    xhr.send(formData);
    
    0 讨论(0)
提交回复
热议问题