Click to See Complete Forum and Search --> : Method for interfacing Javascript (AJAX) with servlet


DEADFA11
07-05-2010, 07:53 AM
Hello there.

A project that I'm working on involves using JavaScript to graph some data that was extracted from a database. The path I am currently exploring involves using an AJAX call (I'm using Flot (http://code.google.com/p/flot/) to do the graphing, so AJAX seems natural) to access a REST Java servlet (specifically a RESTlet (http://www.restlet.org/) for initial prototyping, but I am open to other ideas). The idea is that the servlet will extract the data from the database, serialize it to JSON, and give it to the JavaScript front end for graphing.

The problem I am having at this point is that my servlet is accessed using localhost:port, while, at this stage anyway, my web page front end with the JavaScript is on the hard drive, so I encounter problems with some browsers that think the AJAX call is trying to access a different domain because http://localhost:port# looks different from c:\myDirectory\myServlet.

Note that once the project is complete, both the front end and the servlet will be on the same server, and most likely under the same domain, but for the prototyping stages I don't have access to the server.

A solution I have explored briefly is using a small PHP script on the disk to act as an intermediary, since there are no restrictions on what domains PHP can access. So the idea is that I use the AJAX to call my PHP script, which calls the servlet and returns to AJAX the data it received from the servlet. Seems a little convoluted, but at first glance it appears like it might solve my problem.

I am an experienced programmer, but my exposure to web development is quite limited, so my initial attempts at crafting such a PHP script have been unsuccessful. However, I'm sure I'll be able to construct such a thing, if it is indeed a possible (and practical) solution.

So before I waste my time, is this a reasonable solution, or is there a more straightforward method to tie these two components together that I have overlooked?