It seems like there is no way to implement a JSONP solution (JSON with Padding) using DataSnap, but I want to throw this question here if someone has solved this problem.
Reference Information. JSONP is a mechanism that uses the cross-site capabilities of an HTML script element to overcome the same source policy of the XmlHttpRequest class. Using XmlHttpRequest, you can only receive data (JSON objects) from the same domain that served the HTML document. But what if you want to receive data from several sites and associate this data with browser controls?
With JSONP, your script element src attribute does not reference a JavaScript file, but instead refers to a web method (one that may be in a different domain from which the HTML was derived). This web method returns JavaScript.
The script tag assumes that the returned data is JavaScript files and runs normally. However, what the Web method actually returns is a function call with a literal JSON object as its parameter. Assuming that the function that is being called is defined, the function executes and can work with the JSON object. For example, a function can retrieve data from a JSON object and associate this data with the current document.
All the pros and cons of JSONP have been widely discussed (this is a very serious security issue), so there is no need to repeat it here.
What interests me is that someone there figured out how to use JSONP with Delphi DataSnap REST servers. This is the problem, as I see it. A typical use of JSONP may include a script tag that looks something like this:
<script type="application/javascript" src="http://someserver.com/getdata?callback=workit"> </script>
The getdata Web method will return a call something like this:
workit({"id": "Delphi Pro", "price":999});
and the workit function might look something like this:
function workit(obj) { $("#namediv").val(obj.id); $("#pricediv").val(obj.price); }
The problem is that DataSnap doesn't seem to be able to return a simple string, like
workit({"id": "Delphi Pro", "price":999});
Instead, it is wrapped as shown below:
{"result":["workit({\"id\":\"Delphi Pro\",\"price\":999});"]}
Obviously, this is not JavaScript executable.
Any ideas?