1

I am returing JSON data as a response in a web service I am writing.

I am wondering what is the best way to consume the data, returned to a Javascript function (passed as an argument).

Do I:

  1. use eval()
  2. use Doug Crawfords jSon parser to parse the JSON data into a Javascript object
  3. simply use the returned data as an object literal

BTW, I am still learning Javascript, (just started reading the Definitive Guide 2 days ago) so if my question seems a bit naive or asinine - bear with me.

0

4 Answers 4

2

I would suggest a few things, first, on the web service side:

  1. If a callback=functionName parameter is passed in the query string, then return the properly serialized JSON as the only parameter to the callback function (In this case functionName
  2. If no callback was requested, then simply return the properly serialized JSON string. (This is helpful for when other languages consume the JSON. PHP or Ruby for instance)

Then, as far as consuming it, if you are on the same domain as the web service, you can retrieve the code and use the json2.js parser to parse it. If you are on a separate domain, use the callback parameter when requesting the data, and make sure you set up a function to handle the callback.

Simple example (Using PHP):

<?php
   $callback = null;
   if( isset($_REQUEST['callback']) ){
     $callback = $_REQUEST['callback'];
   }

   $fakeData = array( 'key' => 'value');
   $json     = json_encode( $fakeData );

   echo $callback ? "$callback($json)" : $json;
?>

JS (On different domain than the web service):

function handleCallback( data ){
  alert( data.key );
}

function get_json(){
   // Dynamically create the script element
   var script = document.createElement('script');
   script.type = "text/javascript";
   script.src  = "http://yourwebservice.com/service/?callback=handleCallback";
   document.getElementsByTagName('head')[0].appendChild( script );
}

Calling get_json would dynamically create the request (as a script element due to cross domain restrictions. You can't use XMLHttpRequest to make cross domain requests in most browsers), and the PHP returned script would correctly call the handleCallback function when the script returns.

Sign up to request clarification or add additional context in comments.

1 Comment

The latest versions of all the major browsers support native JSON, so scripts should check for it before loading json2.js.
0

If you are using jQuery you can set it up so it returns as a JSON object. http://api.jquery.com/jQuery.getJSON/

Comments

0

Assuming that your webservice is decorated with ScriptService attribute: I would suggest using json2.js to handle the json clientside. It defers to native implementations when they are available.

Comments

0

(1). use eval()

Only if you're certain the data is clean. I'd never trust anything from an outside source.

(2). use Doug Crawfords jSon parser to parse the JSON data into a Javascript object

Best idea overall. Even jQuery uses plain old eval() to parse JSON.

(3). simply use the returned data as an object literal

When it's returned, it's just a string, not an object unless passed through one of the two aforementioned functions, or wrapped in a function ala JSONP.

2 Comments

@bdl "Even jQuery uses plain old eval() to parse JSON" is incorrect as of jQuery 1.4
@Doug> Thanks for the clarification. Edit: apparently they haven't changed the API docs, because it still claims to use eval().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.