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

366
Visualizações
How do you fake a .xhr request using fetch() and vanilla JavaScript?

I am working on stripping all of the jQuery out of a website and replacing it with plain JS. First off I have no control over the backend. I can not change anything server side. So the api will run my request through this:

def render_created_comment(comment)
    if request.xhr?
      render :partial => "comments/postedreply", :layout => false,
        :content_type => "text/html", :locals => { :comment => comment }
    else
      redirect_to comment.path
    end
  end

This is setup so the site can process normal js requests and those who refuse to use JS and treat the site like it is 1999.

So I need to write a fetch request that fakes being an .xhr , is that possible? I can already send the form data but I end up with the redirect which I am trying to avoid.

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

0

When you use fetch() add the X-Requested-With: XMLHttpRequest header. This is essentially what jQuery and other frameworks do.

fetch(url, {
    headers: {
        'X-Requested-With': 'XMLHttpRequest'
    }
}
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