📌  相关文章
📜  已解决 post 请求中的 cors 错误 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:59.095000             🧑  作者: Mango

代码示例1
let postel = document.getElementById("post");
let postInputel = document.getElementById("postInput")
let postIdInputel = document.getElementById("postIdInput")
let postNameInputel = document.getElementById("postNameInput")
let postAgeInputel = document.getElementById("postAgeInput")
let postResultel = document.getElementById("postResult")

var formData = {
    id:"",
    name:"",
    age:""
}


postIdInputel.addEventListener("change", function(event) {
    formData.id = event.target.value;
});

postNameInputel.addEventListener("change", function(event) {
    formData.name = event.target.value;
});

postAgeInputel.addEventListener("change", function(event) {
    formData.age = event.target.value;
});

const postMethod = () => {
    let options = {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          'Access-Control-Allow-Origin': 'http://localhost:7000'
        },
        body: JSON.stringify(formData)
      
    };
    console.log(formData)
        let url = postInputel.value;
        console.log(url);

        fetch(url, options)
          .then(function(response) {
            alert("DATA INSERTED SUCESSFULLY");
          })
}


postel.addEventListener("submit", function(event){
    event.preventDefault();
    postMethod();
  });