How much an AJAX status zero means

alt-"ajax status zero"
Recently I was working on a project related to a salary calculator. The project was basically targeted towards home-maker moms. The core concept was that to calculate the expected annual salary of moms taking input their number of kids and how much hours do they spend weekly doing homely odd jobs like caretaker, chef, hair stylist etc.
The project comprised of three screens which came to be shown one after another by using Jquery hide()/show() methods. The first screen took the number of kids, age of kids and the states of USA where they belong to. The second screen took the number of hours devoted for doing the jobs. After the submission of the second screen, a cheque would appear wherein the total salary was shown inside a text field. All the details were then stored into the database tables using jquery ajax.(one table for mom details like state, total salary and no of hours for each job and the second table was storing kids details like individual kids ages were stored with a particular mom_id which I used to retrieve from the respective mom table.
But as the project was coming to an end I faced a very peculiar problem. The problem was that when the project was loaded first time into the localhost and after the submission of second form Ajax would throw an error saying something went wrong and the page would again return to the first screen instead of going to the third screen. Checking the database, I came to know that the mom’s database was being inserted but there was no insertion in the kid’s database. What made the problem more interesting was that when the page returned to first screen and subsequent data entered the project run as it was expected to do. Therefore, the real issue was coming when the page was loaded for the first time not the next time. I was facing a problem like this for the first time.
I tried many things that include converting JavaScript arrays to PHP arrays by JSON.stringify() method, then tried to put both the table insertion in a single PHP function and many others. After many failed attempts and being toiled hard for almost 5 hours, I gave the alert a closer look and what it was trying to decipher.
Then I started reading about the Ajax error functions and error handling events available in Ajax.Simply put I just added a single line in the alert statement i.e.; error : function(xhr){ alert(“Some error occurred.Status ”+xhr.status);}
After this, I cleared the browsing data from the browser and run the program again. Now the alert threw a message “some error occurred status:0”. After that I read about status zero of ajax and came to know that it is caused when a connection is not established with the destination or the destined URL can’t be reached or in my case if the screen is altered before the host page gets a response back. Next what I put the page transitioning JavaScript code inside the done function of the response and now my code was running perfectly.

Leave a Reply