lundi 30 novembre 2015

Returning Errors From AJAX Calls in Separate Javascript

Using the Laravel 5.1 framework, I have a situation where my page makes a call that is run through a Request form created with artisan where I have my requirements for the incoming data. This call goes through a function in an included javascript that contains a generic ajax call with parameters allowing its use on multiple pages. My issue is that if the Request throws an error (for formatting, or otherwise), it is returned through the ajax call to the external javascript, and because of the asynchronous nature the function ends before I can get the information back to the calling page where the errors need to be displayed.

Because (to my knowledge), you cannot return from an error/done callback, I've had to resort to moving the function to the page where it can be accessed in another instance of the issue. I'm hoping to avoid that here, though, as this function is used by four different pages and that would result in a lot of replication of code.

So my question is, how can I avoid making ajax synchronous to make a return work, or should I simply move the functions to the calling page?

Apologies if this is unclear, I'm writing in javascript but my understanding of best-practice architecture is still WIP.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire