Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!