【js】原生js进行post请求

【js】原生js进行post请求

let url = 'https://xxxupload'
      let params = {
        "body": {
          "id": row.id
        },
        "channel": "",
        "token": "",
        "userId": "",
        "version": "1.1.0"
      }
      
  
      
      let xhr = new XMLHttpRequest(); // 创建XHR对象
            xhr.onreadystatechange = function () {
              
                if (xhr.readyState == 4) { // 4表示此次请求结束
                    console.log("后端返回的结果:" + xhr.responseText);
                   resolve();
                   
                    /** 你的逻辑代码 **/
                  //  let result = JSON.parse(xhr.responseText);// 后端返回的结果为字符串,这里将结果转换为json
          
                //  console.info(result)
                    /** 你的逻辑代码End **/
                }
            };
            xhr.open( // 打开链接
                "post",
                url, // 后端地址
                true
            );
      // 解决跨域问题
    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
      xhr.setRequestHeader("Content-type", "application/json");
            //xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // 设置请求头
            xhr.send( // 设置需要携带到后端的字段,字符串形式
               JSON.stringify(params)
            );
 

猜你喜欢

转载自blog.csdn.net/G971005287W/article/details/130588617
今日推荐