AJAX Using REST APIs without server-side support

I am creating a read-only REST-based API to access the services of my application. I plan to write a web application that will use these APIs to provide basic information about the status of the application. I plan to use AJAX (using jQuery) to display information.

I originally planned to use Grails, Spring MVC, RoR, or one of the web frameworks to handle the back end of my application. The REST APIs I will provide, although they are already built on the basis of stanalone REST, so I would use only the underlying platform for the main application, and not the business logic. In the future, I may need a server-side structure to handle other tasks, but at the moment most of the work is done in the REST API.

My question is: should I worry about using the server side web application framework? I should be able to make all the API calls I need from AJAX directly from the browser. I can’t figure out what I need to do on the server side. Does it make sense to have the application standard HTML + AJAX + REST?

+4
source share
4 answers

It's hard to say without knowing more about your current setup. Here's what your situation looks like:

  • You already have an application ready to go with all the business logic contained. Looks like it is written in Java?
  • You already have services written and open via REST api using some other standalone framework. This means that if you want, you can access the data right now using a browser without any extra work.
  • You have not created a web application yet, but when you do, it will get all its content from the REST api using XHR and jquery. I say this because otherwise I would think that you are already using some kind of structure to create other content.

If I am right in my assumptions, then I would say that you do not need an additional frame layer. Grails, RoR, SpringMVC use my AJAX and help expose the REST services, but the main part of what they provide is an easy way to make an application that needs to generate html on the server, process forms requests and process sessions in request / response. It really doesn't seem like you will do this, and this is likely to make your application more complex.

If at some point you need things that are rails, etc., I would say that you might not need to use rails to expose the rest of the apis that you currently have. You can use the rails only for what you need and continue to use what you have for the REST api.

+4
source

Well, AJAX calls need to pull data from the server somewhere. If the goal is to avoid complex server-side configuration, CherryPy can keep the server-side code VERY small.

I wrote a simple example. The first class is where you put the logic for your ReST API. The code under the class is all that is needed to start the server.

Install Python 2.6, save the code below for restExample.py. Then, on the command line, run the python file by executing "python restExample.py". Point your browser to http: // localhost: 8080 / blog / 999 and see how the JSON returns.

import cherrypy import json # Create the controller class Blog_Controller(object): def get(self, entryID): cherrypy.response.headers['Content-Type'] = 'application/json' return json.dumps({'title':'Entry Title from DB', 'entry_text': 'Text From DB'}) def update(self, entryID, titleFromPOSTFormInput, textFromPOSTFormInput): # Update DB with passed in arguments. entryID comes from URL, # other two entries come from POST cherrypy.response.headers['Content-Type'] = 'application/json' return json.dumps({'success':True}) # Setup URL routes d = cherrypy.dispatch.RoutesDispatcher() d.connect(name='blog_entry', route='blog/:entryID', action='get', controller=Blog_Controller(), conditions={'method': ['GET']}) d.connect(name='blog_entry', route='blog/update/:entryID', action='update', controller=Blog_Controller(), conditions={'method': ['POST']}) config = { '/' : { 'request.dispatch': d } } cherrypy.tree.mount(root=None, config=config) # Start the webserver engine = cherrypy.engine try: engine.start() except: sys.exit(1) else: engine.block() 
0
source

It seems like this might be a good option for GWT with Restlet .

0
source

Forgive me for making my own horn, but if you are doing AJAX REST using jQuery, you should probably see my JSON-REST plugin:

http://plugins.jquery.com/project/rest

If you get XML back, it will not be so useful, but you can still adapt part of the code to your needs.

0
source

All Articles