The front-end XMLHttpRequest object sets an error in the request header

The front-end XMLHttpRequest object sets an error in the request header

Front end source code

// FormData
var myFormData = new FormData();
myFormData.append('file', fileData);
 
// XMLHttpRequest 
var xhr = new XMLHttpRequest();
 
// Set the request header (actually the order of this is not correct)
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
 
// open(method, url, async)
xhr.open('post', actionData, true);
 
xhr.send(myFormData);

As a result, the console error shown in the figure above appears. It can be seen from the prompt that the XMLHttpRequest object must be opened first.

The easy solution is to XHR.Setrequestheader() after XHR.Open();

Similar Posts: