我想在元素id与数组中的某个值匹配时发送post或delete请求?

loading...


1

所以我试图通过API Build 一个偏好中心,到目前为止我设法获取信息并将其映射到一个复选框,最后一步是在检查复选框时发送一个帖子或删除请求 .

因此,如果正在检查或取消选中该复选框并将该ID放入请求网址的间隙中,我必须以某种方式获取当前元素 .

或许我必须以不同的方式做到这一点 .

如果有人可以帮助我,我真的很感激,因为这是最后一点代码然后它准备好了 .

var geturl = "https://api.sendgrid.com/v3/asm/suppressions/ibrarr2000@gmail.com";
var token = "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";
let headers = new Headers();
let myData = {};
headers.append('Authorization', 'Bearer ' + token);
//supressionids = new Array();
var supressionids = [];

fetch(geturl, {
    method: 'GET',
    headers: headers,
}).then(function (res) {
    return res.json();
}).then(function (data) {
    myData = data;
    runCode(data);
});

function buildSelect(d) {
    let select = document.createElement('form');
    d.suppressions.forEach(function (item) {
        let label = document.createElement('label');
        let option = document.createElement('input');
        option.value = item.suppressed;
        option.type = "checkbox";
        option.setAttribute("onclick","task(event);");
        option.id = item.id;
        option.classList = "subbox";
        option.textContent = item.name;
        label.textContent = item.name;
        select.appendChild(label);
        select.appendChild(option);

        supressionids.push(item.id);

        let br = document.createElement('br');
        select.appendChild(br);

        if(JSON.stringify(item.suppressed) == 'false'){
            option.setAttribute("checked", "checked");
            }
    });
    document.querySelector('body').appendChild(select);  
}

function runCode(data){
    buildSelect(data);
    console.log(supressionids);
}

function task(e) {
  if(e.target.checked){
    fetch("https://api.sendgrid.com/v3/asm/groups/"+ supressionids +"/suppressions/ibrarr2000@gmail.com", {
         method: 'DELETE',
         headers: headers,
        });
  } else {
    fetch("https://api.sendgrid.com/v3/asm/groups/"+ supressionids +"/suppressions", {
      method: 'POST',
      body: JSON.stringify({ "recipient_emails": [ "ibrarr2000@gmail.com" ] }),
      headers: headers,
      });
  }
}

loading...

0回答

No Data

评论

暂时没有评论!