ajax file文件上传

 ajax file文件上传

var formData = new FormData();
var file = $("#leadingInInputVal")[0].files[0];
    formData.append("data",file);
    console.log(formData)
    // formData.append("projectId","2"); //要传的参数
$.ajax({
    url: "http://localhost:8080",   //地址
    type: "post",
	ata: formData,
	xhrFields: {
		withCredentials: true
	},
	crossdomain: true,
	processData: false,   //不需要处理formData参数
	contentType: false,   //需要在请求头追加contentType类型指定
	cache: false,         //阻止浏览器缓存
	dataType: "json",
	success: function(res){
        console.log(res)
    },
    error: function(error) {
        console.log(error)
    }
});

 

你可能感兴趣的:(JS)