Ajax requisition

Asked

Viewed 52 times

-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);
  }

1 answer

1


first, welcome to Stackoverflow :)

The code is simple to notice ...

within an anonymous function of the addEventListener you have 3 functions:

  • success
  • error
  • ajax

the first two do nothing, are just "placeholders", but are called by the function ajax() correctly...

there is a second EventListener that binds the Submit of a form and that’s what he calls the ajax, with

ajax(form.method, form.action, data, success, error);

form.method is what represents usually the method of a <form> form.action is what represents usually the action of a <form>

I mean, augures on the ground there is something like:

var form = document.getElementById("o-meu-formulario");

and HTML has something like:

<form action="/pagina.php" method="get" class="form-example">
    ...
    <button type="submit">Submeter o formulário</button>
</form>

so the code is just fetching form values in HTML...

to see better, you can open the console, and go to "NETWORK", submit the dealer and the call ajax will appear there, if you just want to see by code, change for example

function success() {
    console.log('SUCESSO!');
}

function error(err) { 
    console.log('ERRO!', err);
}

so in the console (F12 in browser) you can see the output of the function...


image in the comment:

  • 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 just explained, that’s what’s in the html inside the tag <form> as action, in my example says /pagina-php... the form.action will fetch the value from there... see here, maybe you understand better: https://i.imgur.com/Rqcvzno.png

  • 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

  • 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...

  • Ok, vlw use Stack overflow just a little while you know how mark the question as solved pq did not find no kkk

  • 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

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.