Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
converting an fetch api call to ajax

i am using a code from a site where onsubmit the call is made and a call is made

$(":submit").click(async (event) => {

i am not aware of this syntax.

my example using the simple $.ajax call and making a post call to the page

so the above function is like this

$(":submit").click(async (event) => {
        event.preventDefault();
        let response = await fetch("page.cfm");
        const reader = response.body.getReader();
        const len = response.headers.get("Content-Length") * 1;

my ajax call is like this

$.ajax({
                url: "page.cfm",
                cache: false,
                data : $('#form').serialize(),
                method: "post",
                beforeSend: function() {
                    $('div.results').html('Please wait');
                },
                success: function(response){
                    $('div.results').html(response);
                }

how can i format the above to my ajax call

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Include form data in the body. Make sure that your await fetch is inside async functions.

$('document').ready( async function() {
  $("#test_fetch").submit( async function(e) {
    e.preventDefault(); // avoid to execute the actual submit of the form.
    var form = $(this);
    var url = form.attr('action');
            
    let response = await fetch(url,{
      method:"post",
      body: $(form).serialize()
    });
    ...
  });
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You have to change the request to POST and pass the data.
Then read the data from the response

$(":submit").click(async (event) => {
    event.preventDefault();
    $('div.results').html('Please wait');
    let response = await fetch("page.cfm", {method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: $('#form').serialize()});
    const html = await response.text();
    $('div.results').html(html);
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda