玩轉圖片上傳————原生js XMLHttpRequest 結合FormData對象實現的圖片上傳


var form=document.getElementById("formId");
        var formData=new FormData(form);
        var oReq = new XMLHttpRequest();
        oReq.onreadystatechange=function(){
          if(oReq.readyState==4){
            if(oReq.status==200){
                var json=JSON.parse(oReq.responseText);
                console.log(json);
            }
          }
        }
        oReq.open("POST", "http://localhost:3000/doup");
        oReq.send(formData); 

  該方法是可以跨域的(端口不同的跨域),親測可用!

  服務端如何處理的解決方案看我的這篇文章http://www.cnblogs.com/theone67/p/6698762.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM