Finding an Error

I see lots of people asking for help with finding errors in their code where they don't even know whether the error is in the browser or on the server. This means that they don't even know what forum to post their request for help in as most forum sites have a number of different forums depending on where the code is running and what language it is.

The first answer they should get to their request for help in these cases it that they need to work out whether the error is on the server on in the browser before any actual help can be given.

Where their processing actually involves two separate web pages, the way to determine whether the problem is in the browser side validation on the first page or in the server side processing on the second page is very simple. All they need to do is to dump out all the values received by the second page and see if they are what is expected. If they are then the problem is in the server code following. If they are not then the problem is in the browser code on the first place.

So if they are using PHP on the server and are using a form to post values to the second page then they need to use the following statement at the top of the code in the second page to see what that page is receiving.

var_dump($_POST);

Other server side languages will usually have an equivalent statement that can be used to dump out the values received into the page.

If the processing is all being done within the one page with an 'ajax' call to the server being made to retrieve data from the server without loading a whole new page then locating whether the problem is in the browser or on the server is slightly more complicated.

In this situation we need to temporarily modify the code in both places to enable us to see whether the values are reaching the server or not. First we need to make sure that the server side code exits straight after outputting whatever was passed to it so that we know that what server side code we are running in our test will actually return if it gets called. We then need to temporarily replace the onReadyStateChange code so that all it does is to dump whatever is passed back to the console (comment out the proper code so it can easily be put back). Here's an example of what you might use as your temporary onreadystatechange code.

req = new XMLHttpRequest();
req.onreadystatechange = function() {
var cType;
if (this.readyState == 4) {
if (this.status != 200) console.log('Error : Status '+this.status+' returned.');
else console.log(this.responseText);
}
}

If that returns the correct values you wanted passed to the server then you know that the code making the request works properly (if it doesn't then that part of the code contains the error). Having determined that the server is receiving the right values you can then change the server side code back the way it was but leave the onreadystatechange with the temporary code for the next test. This will then tell you whether the correct value is being passed back to the browser. If so then you know that the problem is in your original onreadystatechange code and if not then you know the problem is on the server.

By carrying out these tests before asking for help you can narrow down where the problem is and so know which forum to post on to ask for help. By including the information on the tests you have already run to narrow down where the problem is you will then be far more likely to receive useful help in actually finding the problem. Others are always far more likely to help when you have done something to try to find the problem and are genuinely stuck than if you simply expect them to debug your entire code for you.

go to top

FaceBook Follow
Twitter Follow
Donate