Skip to content

HTTP requests with Javascript

Thomas Starzynski edited this page Jun 1, 2019 · 7 revisions

Custom HTTP requests with javascript

in view

<input id="auth" type="hidden" value="<%= session[:_csrf_token] %>">

const authenticityToken = document.getElementById("auth").value;

const sendPostRequestToCreateReko = (card) => {
  const xhr = new XMLHttpRequest();
  // listener for server response
  // whenever the request gets a response the .onload() function gets triggered
  xhr.onload = function () {    
  };
  // DATA TO SEND
  const toSend = {
    authenticity_token: authenticityToken,
    feedback: {
      content: TODO
    }
  };
  xhr.open("POST", "/rekos");
  xhr.setRequestHeader("Content-type", "application/json");
  xhr.send(JSON.stringify(toSend));
  // console.log("POST request send with data: ");
  // console.log(toSend);
};

Clone this wiki locally