form.submit()提交后返回数据的处理

form.submit()发送请求一般是单向的,如果需要取返回的数据,一般会发送ajax请求,但是如果form中有附件呢?(以后有时间给大家分享ajax上传附件的功能),确实需要返回数据来知道该功能是否执行成功呢?我的解决方法是在form 中增加一个target属性,让其返回的数据添加到一个隐藏的iframe的控件中,返回的数据

 <label for="a"> 上传附件 </label>  
               <form id="uploadForm"  enctype="multipart/form-data" target="frameFile"  method="post">
             
                 <input type="file" id="a" name="a" onchange="fileUpload()" style="position:absolute;top:0px;right:0px;cursor:pointer; opacity:0;filter:alpha(opacity:0);z-index:999;"    />
                         
              </form>
                <iframe id='frameFile' name='frameFile' style="display:none">    
                </iframe>

以下是返回页面中后台返回数据的处理

<script type="text/javascript">
    try {
        var data = eval("($result)")

        if (data.success) {
            alert(data.res)
        }
        window.top.LoadByFrame(data.success);
    } catch (e) {
        window.top.closeBg();

    }
</script>
function fileUpload()
    {
        var form = document.getElementById('uploadForm'); 
        form.action="XXX.do?";
        form.submit();
    }

这样就能够在隐藏的iframe中显示处理过的数据了 

posted @ 2019-05-27 18:04  心有玲曦遇奇缘  阅读(18934)  评论(0编辑  收藏  举报