-
Notifications
You must be signed in to change notification settings - Fork 1
HTTP requests with Javascript
Thomas Starzynski edited this page Jun 1, 2019
·
7 revisions
encode the autheticity token
<!-- yourpage.html.erb -->
<input id="auth" type="hidden" value="<%= session[:_csrf_token] %>">// grab the encoded authenticity token
const authenticityToken = document.getElementById("auth").value;
// send custom Post Request
const sendPostRequest = () => {
const xhr = new XMLHttpRequest();
// whenever the request gets a response the .onload() function gets triggered
// its like an event listener
xhr.onload = function () {
};
// DATA TO SEND
const toSend = {
authenticity_token: authenticityToken,
feedback: {
content: TODO
}
};
xhr.open("POST", "/feedbacks");
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(JSON.stringify(toSend));
// console.log("POST request send with data: ");
// console.log(toSend);
};# feedbacks_controller.rb
def create
# now you can acces the send data and process it!
params[:feedback][:content]
end