zoukankan      html  css  js  c++  java
  • js中POST格式为base64的数据

    参考: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保留了其中的特殊符号

  • 相关阅读:
    前端面试集锦
    nodeJs上传附件
    逻辑于 逻辑或
    webpack 学习笔记 (一)
    yum 安装mongodb mysql
    闭包面试提 (2)
    主动的重要性
    1.1一天一题:逆转字符串
    编程提高:一天一道编程题
    iconv将文件编码从gb2312 转换为utf-8
  • 原文地址:https://www.cnblogs.com/cekong/p/11476864.html
Copyright © 2011-2022 走看看