form+iframe+file 页面无刷新上传文件并获取返回值

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="aa">上传</button>
<form id="ajaxUploadform" action="/api/upload" role="form" method="post" target="ajaxIframe" enctype="multipart/form-data">
<input type="file" id="ajaxFile" name="ajaxFile"/></form>
<iframe name='ajaxIframe' id='ajaxIframe' ></iframe>
<script>
var aa=document.getElementById("aa");
aa.addEventListener("click",function(){
document.getElementById("ajaxUploadform").submit()
document.getElementById("ajaxIframe").onload=function(){
var result=document.getElementById('ajaxIframe').contentWindow.document.body.innerText;
alert(result)
}
},false)

</script>

</body>
</html>
原文地址:https://www.cnblogs.com/liuhao-web/p/9987199.html