Friday, September 14th, 2007

JSONRequest Extension for Firefox

Category: JavaScript, JSON, Library

Collin Jackson has written a JSONRequest extension for Firefox that exposes the JSONRequest communication API to web pages you visit.

It does this by adding a new window.JSONRequest object to your world.

An example of using this beast is on the main page itself:

javascript

  1. function request(method, data) {
  2.   var timeout = document.getElementById("timeout").value;
  3.   var url = document.getElementById("url").value;
  4.   var requestNumber;
  5.   if (timeout != "") {
  6.     if (method == "get") {
  7.       requestNumber = JSONRequest.get(url, done, timeout);
  8.     } else if (method == "post") {
  9.       requestNumber = JSONRequest.post(url, data, done, timeout);
  10.     }
  11.   } else {
  12.     if (method == "get") {
  13.       requestNumber = JSONRequest.get(url, done);
  14.     } else if (method == "post") {
  15.       requestNumber = JSONRequest.post(url, data, done);
  16.     }
  17.   }
  18.   document.getElementById('results').innerHTML +=
  19.     "Sending request " + requestNumber + "...<br />";
  20. }

JSONRequest Firefox

Posted by Dion Almaer at 9:49 am
9 Comments

+++--
3.4 rating from 24 votes

9 Comments »

Comments feed TrackBack URI

This is great news. With CrossSafe and this, we now have a native solution as well as a pure JS partial implementation of the JSONRequest. This is a great combination, because you could use CrossSafe, and it will automatically defer to native JSONRequest (like this) when available, otherwise it will do itself.
However, Colin, it appears that it behaves a little different than I expected, the JSONRequest object is not available until after the page is finished loading, so inline scripts can’t access JSONRequest immediately. Is there any reason for this? I would like to make sure CrossSafe works properly with your extension (properly defer to the plugin when available).

Comment by Kris Zyp — September 14, 2007

I don’t get it. Why is this worth an extension when we can easily serialize and deserialize in JS already?

Comment by Calvin Spealman — September 14, 2007

I’m following up with Kris offline to come up with a solution to the load timing issue.

Calvin, the short answer to your question is that JSONRequest lets you retrieve information across domains (unlike XMLHttpRequest) and safely (unlike the cross-domain script tag). For more information, see the JSONRequest proposal page.

Comment by Collin Jackson — September 14, 2007

Good news! I hope JSONRequest become a de facto standard ;)

Comment by Andrés Testi — September 14, 2007

Why the code is not submitted as patch for bug 360666.

Comment by Biju — September 14, 2007

Great news and great that this is getting coverage in the community… (thanks Ajaxians). … JSONRequest seems very like a very reasonable solution and straightforward to implement.

Comment by Mark Holton — September 14, 2007

I’ve released a new version of the JSONRequest extension (version 0.5) to handle the issue that Kris brought up.

Comment by Collin Jackson — September 15, 2007

This is sad.

Doug and others agreed that JSONRequest was the wrong way to solve this problem. The right solution is to use XMLHttpRequest2 which is available in nightly builds of Firefox3.

http://www.w3.org/TR/access-control/
https://bugzilla.mozilla.org/show_bug.cgi?id=389508

Comment by Erik Arvidsson — September 18, 2007

I am strongly in favor of the JSONRequest. It is the right way to solve the problem. I dislike the XMLHttpRequest2 because it is confused about trust boundaries, and as we have repeatedly learned in the trenches, security is rarely obtained with confusion.

Comment by Douglas Crockforrd — September 18, 2007

Leave a comment

You must be logged in to post a comment.