JSONP with Dart

185 views
Skip to first unread message

Kris

unread,
Nov 22, 2011, 5:13:14 PM11/22/11
to General Dart Discussion
Hi,
A newbie to the Dart world. Trying to replace the JS scripts (which
use JQuery) for my app with Dart as an exercise. Since I used JQuery's
AJAX capabilities, I am stuck with how to leverage JSONP (I need to
make some cross domain calls) in the Dart implementation. Can someone
throw a few pointers as to how I can go about it?

Thanks.

Bob Nystrom

unread,
Nov 29, 2011, 4:29:27 PM11/29/11
to Kris, General Dart Discussion
Hey, sorry for the long delay. This fell through the cracks.

I don't think we've thought about JSONP much, if at all. In general, Dart isn't as dynamic as JS about executing arbitrary strings of code in arbitrary contexts. Instead, when you want to execute some new Dart (or JS) code at runtime, you need to do so in a new isolate.

This is outside of my area of expertise, but I'd take a look at the isolate API and see if you can bend that to your will. You may be able to, or it may just be an exercise in frustration right now. It's a region of the language that's still under heavy development.

- bob

Michal Mocny

unread,
Dec 1, 2011, 2:17:56 PM12/1/11
to General Dart Discussion
I asked a very similar question earlier, and here are some
conclusions:

jsonp works by wrapping the resulting json with a javascript callback
and then sourcing that resulting text as a javascript "script". Due
to Darts similar-to-js syntax, optional typing, and embeddable json,
its very possible that the resulting text could parse as if it were
Dart, but its not guaranteed. To be safe, you would need Dart
versions of the server call, which most services don't provide (One
person suggested that you could implement your own server proxy that
would speak "dart-jsonp"). However, even if you had a server that did
support "dart-jsonp", current browsers don't have dart vm's. As for
using the dartc, the javascript which dart compiles has mangled
function names (and possibly worse: inlined/altered function
signatures, etc) as so you cannot reliably use that code in a
javascript callback which you need for jsonp.

So, I never did find a valid solution. Sorry.

Bob: I would really suggest/request that the team think about jsonp a
little. Thanks!

On Nov 29, 4:29 pm, Bob Nystrom <rnyst...@google.com> wrote:

Ali Afshar

unread,
Dec 3, 2011, 8:06:53 PM12/3/11
to Michal Mocny, General Dart Discussion
Hi, I achieved this simply by adding some JavaScript to the page for
the callback function as a bridge back to Dart via a window message or
event handler. It sucks pretty much, but who says we can't mix in a
bit of JavaScript until a valid solution arises.

--
Ali Afshar | www.googplus.org/ali | Google Developer Relations

Michal Mocny

unread,
Dec 4, 2011, 10:49:53 AM12/4/11
to Ali Afshar, General Dart Discussion
Ali, would you mind pasting an example?

Ali Afshar

unread,
Dec 4, 2011, 5:19:51 PM12/4/11
to Michal Mocny, General Dart Discussion
Hi Michal,

Sure, I am writing a blog post, but for now consider this untested but
simplest example I could think of:

<html>
<body>
<script type="text/javascript" src="Jsonp.dart.app.js"></script>
<script type="text/javascript">
function callback(s) {
window.postMessage(s, '*');
}
</script>
</body>
</html>

class Jsonp {

void run() {
window.on.message.add(codeReceived, false);
ScriptElement s = document.createElement('script');
// Jsonp call
s.src = 'http://my-api-uri?callback=callback';
document.nodes.add(s);
}

void codeReceived(MessageEvent e) {
// e.data is your Json

Michal Mocny

unread,
Dec 4, 2011, 10:39:19 PM12/4/11
to Ali Afshar, General Dart Discussion
That is extremely clever!  I had not known about window.postMessage, and is a very interesting way to achieve message passing between dart+js.

Thanks a bunch!

Ali Afshar

unread,
Dec 5, 2011, 9:05:38 AM12/5/11
to Michal Mocny, General Dart Discussion
I don't think it's that clever really, but I am guessing you can also
use the DOM to store the data, for example by setting the value of a
(hidden) form element and reacting to its change event.
Reply all
Reply to author
Forward
0 new messages