Convert a form action call to Xios

Asked

Viewed 227 times

0

good night

I’m having trouble converting this code

<form action="https://www.personal.com.py/ApiComerciosMaven/webresources/autenticacion/" method="post">
      <p>
        AUTENTICAR USUARIO
      </P>
<p style="font-size:medium;font-weight:bold;">USUARIO
<input type="text" name="usuario" value="test">
</p>
<p style="font-size:medium;font-weight:bold;">CLAVE
<input type="text" name="clave" value="test">
</p>               
<p>
<input type="submit" value="AUTENTICAR"/>
</p>
</P>
</form>

for Xios, I tried this

    axios.post('https://www.personal.com.py/ApiComerciosMaven/webresources/autenticacion/', {usuario: 'AA', clave: 'AA'}).then(req => {
  console.log('Req done: ', req)
}).catch(err => {
  console.error('Error: ', err)
})

and so

    const data = new FormData();
data.append('usuario', 'AA');
data.append('clave', 'AA');


axios.post('https://www.personal.com.py/ApiComerciosMaven/webresources/autenticacion/', data).then(req => {
  console.log('Req done: ', req)
}).catch(err => {
  console.error('Error: ', err)
})

however the request with html returns me the expected date but with Axios no, how could I convert this html into an Axios request?

thanks in advance

  • Try to send via get with params: axios.get('https://www.personal.com.py/ApiComerciosMaven/webresources/autenticacion/', { params: {usuario: 'AA', clave: 'AA'}}).then(

2 answers

0

good morning, if I use get it returns me a 404 error, it has to be post even, when using post by Xios it performs the request successfully, however it does not bring me the same result as doing by browser.

  • When in doubt you posted that there was a problem in Xios, and now you posted that you have a problem in HTML. The returns differ?

  • the request in html works, what I can’t do is the same with Axios, I tried to use fetch, but it returns me the same result as Axios

  • What it returns there on the console.log?

  • in Axios: codigo: 0, Mensaje: "" in html: codigo:0, Mensaje: "asdsada87d89a7d9as7das98d7as9" <= token

  • The Axios he makes a pre requisition called pre Flight, I do not know if it has influence. Have tried a test with jQuery ajax.

  • I’ll run some tests with ajax, but I shouldn’t interfere theoretically

Show 1 more comment

0

The Axios he makes a pre-requisition called pre Flight, I don’t know if this >has influence. Have tried a test with jQuery ajax.

thank you very much richellyitalo

Solution

var ajax = new XMLHttpRequest();

// Seta tipo de requisição: Post e a URL da API
ajax.open("POST", "minha-url-api", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

// Seta paramêtros da requisição e envia a requisição
ajax.send("[email protected]");

// Cria um evento para receber o retorno.
ajax.onreadystatechange = function() {

  // Caso o state seja 4 e o http.status for 200, é porque a requisiçõe deu certo.
    if (ajax.readyState == 4 && ajax.status == 200) {

        var data = ajax.responseText;

    // Retorno do Ajax
        console.log(data);
    }
}

Browser other questions tagged

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