如何发送带有文件附件的表单(JS / jQuery)

时间:2020-07-08 01:25:16

标签: javascript jquery

我一直试图发送包含其他表单数据的文件。但是每一次,我都会得到其他请求的数据,但是文件仍然丢失。我真的不知道如何附加它。我将把脚本放到现在为止,如果有人知道如何帮助我,那就太好了。

function applyJSON() {
  let result = document.querySelector(".result");
  let name = document.querySelector("#form-name");
  let email = document.querySelector("#form-email");
  let phone = document.querySelector("#form-phone");
  let jobPost = document.querySelector("#form-jobPost");

  
  // Creating a XHR object
  let xhr = new XMLHttpRequest();
  let url ="my_API";

    // open a connection
  xhr.open("POST", url, true);

  // Set the request header i.e. which type of content you are sending
  xhr.setRequestHeader("Content-Type", "application/json");

  // Create a state change callback
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      // Print received data from server
      result.innerHTML = this.responseText;
    }
  };



  //Converting JSON data to string
  var data = JSON.stringify({
    "name": name.value,
    "email": email.value,
    "phone": phone.value,
    "jobPost": jobPost.value,
  });

  
  // Sending data with the request
  //uploadFile(data, xhr);
  console.log(data,);
  xhr.send(data,);

  //Clearing fields
  document.getElementById("form-name").value = "";
  document.getElementById("form-email").value = "";
  document.getElementById("form-phone").value = "";
  document.getElementById("form-jobPost").value = "";
  
}

就像我说的那样,脚本中与文件没有任何关系。

0 个答案:

没有答案
相关问题