Convert a call form action to axes

0

Good evening

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 axios, 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)
})

But the html request returns me the expected date but with the axios no, how could I convert that html into an axios request?

Thank you in advance

    
asked by anonymous 29.08.2018 / 03:37

2 answers

0

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

    
29.08.2018 / 14:58
0
  

The axios it does a pre requisition called pre flight, I do not know if that> has influence. Already 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);
    }
}
    
29.08.2018 / 17:24