Good practice for interacting web browser with RESTful API?

I have been using a couple of RESTful APIs for interaction between servers and servers recently, and now I am thinking of communicating directly with RESTful APIs via javascript from a web browser in my web application.

This would mean using ajax on the web page to communicate with my web server using GET, POST, PUT and DELETE requests, and the server would respond with the appropriate HTTP status codes and non-html data (possibly json)

Is this common practice for a web application and why?

+4
source share
1 answer

It doesn't matter if you use the RPC or RESTful APIs from an ajax perspective, but overall you might think of the RESTful API as a well-organized, well-named collection of remote procedural calls.

Is this common practice for a web application and why?

This is useful because you do not need to duplicate code in order to have regular CRUD operations for multiple data objects.

Another thing to keep in mind is that if you have a consistent convention of API call names with which you can write AJAX functions to interact with, you will write and maintain much less code over time on the JavaScript application side. assuming you don’t do anything strange in your code.

An example of when / as it would be good practice would be if you wrote a basic method designed to automatically determine your AJAX URL depending on what you are doing and where you are, and it automatically determines that POST method to use depending on the type of operation ... then you literally write one ajax function and apply it to things, rather than writing completely separate ajax methods for each action element.

+3
source

All Articles