jQuery and Laravel are amazing libraries and frameworks but they don’t always inspire the best practices. Recently when building a basic app I made a fairly amateurish error without even realizing it at first. In building a simple Create/Edit route I used the Route::resource as a shortcut and for making AJAX requests to the route I used $.ajax() on page. The requests work great and everything seems to working fine until sending a large request and right then I realize $.ajax() is using a GET request rather than POST. There are a couple lessons here, you should have XHR requests properly outputting to console.log() so you see what your AJAX requests are doing. But more than that don’t expect jQuery and Laravel to do all the lifting through their syntactic shortcuts. At times, being more verbose with your code can avoid some nasty mistakes.
Explaining your Resource route with a couple ::get or ::post routes looks messy but actually can help to both avoid errors and aid in development by being more obvious to understand quickly.
As well, $.ajax() in jQuery is an all purpose request and specifying your Type can save headaches and keep requests from falling into the wrong lane.
Leave a Reply