js 模拟ajax方式提交数据

html页面

<script>
function LocaluploadCallback(msg) {
    document.getElementById("f_localupload").outerHTML = document.getElementById; //清空
    alert(msg);
}
</script>

<form id="formlocalupload" action="/album/upload/" enctype="multipart/form-data" method="post" target="hidden_frame">
    <input type="file" name="f_localupload" id="f_localupload" accept="image/jpg,image/jpeg,image/gif,image/png,image/bmp" />
    <input type="submit" name="submit" value="提交" />
</form>

<iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>

后台程序

def upload(request):
    try:
       user = request.user
        #file = request.FILES.get('f_localupload')
        file_obj = request.FILES.values()[0]
        return HttpResponse("<script>parent.LocaluploadCallback(‘ok’) </script>")
     except Exception, e:
        log.error(e)
        return HttpResponse("<script>parent.LocaluploadCallback('error') </script>")

原文地址:https://www.cnblogs.com/weiok/p/5115027.html