FormData solves the problem of ajax uploading files and ajax cross-domain

The FormData object can be used to assemble a set of key/value pairs for sending requests with XMLHttpRequest. It can be more flexible and convenient to send form data, because it can be used independently of the form. If you set the encoding type of the form to multipart/form-data, the data format transmitted through FormData is the same as the data format transmitted by the form through the submit() method

Html code section

<div id="uploadForm">
    <input id="file" type="file" name="musicname"/>
    <button id="upload" type="button">upload</button>
</div>

javaScript

	var formData = new FormData();  
	formData.append('file',document.getElementById("fileupload").files[0]);  
	formData.append('test','test23333');
	$.ajax({
		url: "http://192.168.199.100:8080/api/file/upload",
		type: "POST",
		data: formData,
		contentType: false,
		processData: false,
		dataType: "json",
		success: function(result){
			if (result){

			}
		}
	});

Set on the server side (provide the interface)

response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST,GET");

This makes it easy to solve cross-domain problems

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325461432&siteId=291194637