开发者

Should REST webservices be called directly by Ajax or via Servlets/JSPs

I am creating a web service which uses REST web services. The client side code is written in HTML/JavaScript. My dilemma is whether I

  1. should use the REST resource directly using AJAX calls?

    or

  2. 开发者_开发知识库
  3. should I create Servlets/JSPs (where REST calls will be made and data will be sent to client(AJAX/JAVSCRIPT)).

I have seen many web apps which follow the 2nd procedure but seems to me that it's doing the same thing as 1st in an indirect way.

Is there any advantage of using 2nd procedure over first?

What is the standard way to use REST services by HTML/javaScript client?

Please let me know if I am even thinking in the right direction and if not please give your valuable insight.


You can use either approach but note that browsers will enforce the same-origin policy on scripts, so if the REST service lives on a different domain than the script you will need to use a servlet/script on the same domain as the script to proxy the call to the other domain. I suspect this is why you are seeing the second approach used.

A proxy/middle-man servlet may also be useful if not all of the response is needed; you could use the servlet to strip out information that is not needed by the JavaScript to reduce the amount of data sent to the browser.


Directly accessing the resource(s) via AJAX has the obvious benefit of less overhead and is IMHO the more elegant solution, however it is also important to note that not all browsers support PUT and DELETE requests natively.

To get around this, you'll likely want to support the common "_method" hack. This stackoverflow question mentions this approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜