-1
Hello, I have an Ajax code that I don’t understand what’s going on if anyone can help ... I’m not getting xhr.open(method, url); what URL does it set? I have this code and it’s working but I copied want to change some things is my impression or is null? Because it’s working so it can’t be null right? And if it’s not null which URL he’s setting pq wasn’t so for example xhr.open(method, 'whatever.php') or something like that?
window.addEventListener("DOMContentLoaded", function() {
// get the form elements defined in your form HTML above
var form = document.getElementById("form");
var button = document.getElementById("rodar");
// Success and Error functions for after the form is submitted
function success() { }
function error() { }
// handle the form submission event
form.addEventListener("submit", function(ev) {
ev.preventDefault();
var data = new FormData(form);
ajax(form.method, form.action, data, success, error);
});
});
// helper function for sending an AJAX request
function ajax(method, url, data, success, error) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if (xhr.status === 200) {
success(xhr.response, xhr.responseType);
} else {
error(xhr.status, xhr.response, xhr.responseType);
}
};
xhr.send(data);
}
But what URL does it pass in xhr.open(method, url) because it wasn’t supposed to be 'anything.php' or something? 'Cause instead he just passed url I couldn’t figure it out
– Dakota
@Dakota just explained, that’s what’s in the html inside the tag
<form>
asaction
, in my example says/pagina-php
... theform.action
will fetch the value from there... see here, maybe you understand better: https://i.imgur.com/Rqcvzno.png– balexandre
Inside the action tag is this https://formspree.io/xdokagnl is for sending email you know? He is sending the email, I just want to know because when I click on the button he sends the normal email only that does not open anything he sends more does not open, explain me better please
– Dakota
if you are a https://formspree.io/ this is a service that creates Formularios... You are sending all the data you enter for this service... what does this service properly, I can’t say, but see more information on their side...
– balexandre
Ok, vlw use Stack overflow just a little while you know how mark the question as solved pq did not find no kkk
– Dakota
After some time a "visa" will appear under the vote counter of the reply, at that time you can mark as a sure-answer :) as here https://i.imgur.com/k9scqjo.png
– balexandre