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

333
Visualizações
How to tell which ajax request failed?

I have this code block to post a HTTP request via jquery .post() method.

$.post("/product/update", formPostData)
.done(function (data) {
    // success
    alert(data.product_id + ' was updated!');
})
.fail(function (data) {
    // fail, but which request?
});

When it is successful it is easy to know which request we are dealing with, since the json returned by the server has the 'product_id' that I need.

But if it fails due to an error in which the server is not responsive, a connectivity problem for example, how can I tell which request has failed?

The data object has no clues because it only contains the server response.

How can I pass a value to the .fail() handler so I can determine which request has failed?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The this object is useful here. You should be able to parse this.data and get your post information from there:

.fail(function (jqXHR, textStatus, errorThrown) {
    console.log(this.data);
});
over 4 years ago · Santiago Trujillo Relatório

0

Another alternative would be write to the XHR object itself, and then it's there for you in the fail method, and wouldn't require you to use a closure:

var obj = { a: 1 };
var xhr = $.post("/product/update", obj)
   .done(function (data) {})
   .fail(function (jqXHR, textStatus, errorThrown) {
        console.log(this.data); // The raw string a=1 which was posted
        console.log(jqXHR.postData); // The {a: 1} javascript object
   });
xhr.postData = obj;
over 4 years ago · Santiago Trujillo 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