AJAX Errors - Success but failure

Mar 22, 2017 ajax success failure error laravel csrf response
WhatsApp
Spending time on errors are extremely time consuming. I’m not sure what the best ways are to speed this up but some ways are to check the basics and read the errors. Sometimes it’s like stepping in the shoes of Sherlock Holmes but usually most of the clues are in front of you.

In this instance I was missing a column on my database table. I was assigning a variable to a column that didn’t exist. Despite debugging the code and removing lines that caused the error and checking each column with those in my code, I still skipped the missing column. At the time I had my AJAX set up and it was difficult to see the response I was given through the error function as it was not formatted; each line was following the next. But once I had exhausted my trial and error and deductive reasoning, I went over the error response line by line to find that the email was the missing column. It didn’t occur to me that the missing column was the email as I had also been passing it with a hardcoded email to save me time from inserting data into the textfields.

Not only that before the database error I was halted by a CSRF token error as I was not getting any response at all. It would be better to set up a standard POST form first before using ajax as I’d be able to clearly identify a number of things -



That the URI is correct
The CSRF token has been passed
The error response is correctly formatted and easily readable
The success response is correctly formatted and accessible



Finally upon reaching a successful response but failure was in regards to point 4. The success is that the code on the server was correct - the object was created and saved to the database. Each time I’d reload the page and enter in my details it would create a new row. But the response wasn’t formatted correctly. I should remind myself that using the response()->json(); passes a JSON object to the client side and there is no need to JSON.stringify() or JSON.parse() the response. Though JSON.stringify() is useful to print out the response and JSON.parse() is useful to convert a string that is currently using JSON syntax to be converted to a JSON Object. Laravel does convert my model/class to JSON for me however with the response()->json() method.

If you found the following info helpful, I'm happy to accept any donations of the following cryptocurrencies.

  • Bitcoin - 17DTiPExzP9StqveW428acEyB4mVMfKbiK
  • Ethereum - 0x87B8307FD20dc90cc05c94905Ec593134D32B6FF
  • Litecoin - LZMiz5U5sVq9doMLYE3gfLJrxCQDKuyCmU
  • Neo - AXv71WB38ajc1KUUEnxQKhynLLPc4BapVb