參考:https://segmentfault.com/q/1010000000438322
let url = "https://xxx" var httpRequest = new XMLHttpRequest();//第一步:創建需要的對象 httpRequest.open('POST', url, true); //第二步:打開連接 httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//設置請求頭 注:post方式必須設置請求頭(在建立連接后設置請求頭) httpRequest.send("image=" + encodeURIComponent(base64img))//發送請求 將情頭體寫在send中 httpRequest.onreadystatechange = function () {//請求后的回調接口,可將請求成功后要執行的程序寫在其中 if (httpRequest.readyState == 4 && httpRequest.status == 200) {//驗證請求是否發送成功 var json = httpRequest.responseText;//獲取到服務端返回的數據 console.log(json); } };
如果不用encodeURIComponent,base64img數據格式總是錯誤(httpRequest.send時數據中的等號總是消失,encodeURIComponent保留了其中的特殊符號)
