0

I need to save a response made by a @restcontroller in a javascript variable.

you can get the response by invoking an URL like localhost:8080/JSORresponse

and you will get a JSON .

how can i save that json in a javascript dataObject to use it as a source in a library?

Thanks, ask if not clear or you need more details

OLD EDIT - I've got this code:

  var beer;
  $.getJSON("http://localhost:8080/timeline",function(json){
      beer = json;   
      checkDrink();                
  });         

  function checkDrink() {
      console.log(JSON.stringify(beer));
  }   

  var beerS = JSON.stringify(beer);

    var timeline_config = {
     width: "100%",
     height: "100%",
     source: beerS
    }

console.log(JSON.stringify(beer)); log the exactly JSON i want to save in my beerS variable, but when i use it seems not to work, some advice to save that exact log result into a variable and pass it in the timeline_config?

NEW UPDATE - that's the new checkpoint

var beer;
      $.getJSON("http://localhost:8080/timeline",function(json){
          beer = json;   
          checkDrink();                
      });         

      function checkDrink() {
          console.log(JSON.stringify(beer));
          var beerS = JSON.stringify(beer)

          var timeline_config = {
           width: "100%",
           height: "100%",
           source: beer
          }
      }   

but now i got all white page, that's the DOC of the library maybe can help

Source

source Should be either the path to the JSON resource to load, or a JavaScript object corresponding to the Timeline model.

Here is an example using a data object:

 var dataObject = {timeline: {headline: "Headline", type: ... }}
    createStoryJS({
        type:       'timeline',
        width:      '800',
        height:     '600',
        source:     dataObject,
        embed_id:   'my-timeline'
    });

If source is a string, we will try to automatically recognize resources that are Twitter searches, Google Spreadsheets or Storify stories. Failing that, we assume the source is either JSON or JSONP. If string matches on .jsonp, we will treat it as JSONP, otherwise, we will append ?callback=onJSONP_Data. See more details below.

i have also tried using beer and beerS but result remains the same

Mattew Trincanati
  • 191
  • 1
  • 1
  • 9
  • 2
    `var obj = JSON.parse(json);` – Andy Feb 23 '15 at 14:15
  • var obj = JSON.parse(localhost:8080/JSORresponse) would be ok? – Mattew Trincanati Feb 23 '15 at 14:21
  • @MattewTrincanati, probably not, because your REST call will most likely be asynchronous. You'll need to use an XMLHTTP request or an AJAX method from a library like jquery to get at the data before assigning it to a variable. – Andy Feb 23 '15 at 14:43
  • 1
    what is the ContentType returned by the service? If 'application/json', you can try with $.getJSON which will actually return a JSON object without the need to parse it – Carlo Moretti Feb 23 '15 at 15:10
  • It's a JSONresponseTimeline(B), B is a Bean – Mattew Trincanati Feb 23 '15 at 15:29
  • result gives a JSON : {"timeline":{"headline":"Timeline example","type":"default","date":[{"startDate":"1996","headline":"Hello Date Timeline!"}]}} – Mattew Trincanati Feb 23 '15 at 15:30
  • 1
    If the `dataType` is `json`, jQuery should convert the data to a Javascript object for you. Additionally you do not need to do both `ajax` and `getJSON` they both are doing a request to your server. `jsonData` should be the object you want. http://api.jquery.com/jquery.ajax/ – Danny Feb 23 '15 at 15:42
  • check the response ContentType, it's a header of the response, use some REST client tool to check for it if you don't have a way to set it from your server. – Carlo Moretti Feb 23 '15 at 15:43
  • 1
    Ajax is **asynchronous**. – Felix Kling Feb 23 '15 at 15:52
  • why are you setting a string in source : beerS instead of just beer? what is the console output if you log just beer without the stringify? Also as stated by Falix Kling the call is async so beer will be available only from the $.getJSON function on – Carlo Moretti Feb 23 '15 at 15:52
  • the log of beer is : Object {timeline: Object} – Mattew Trincanati Feb 23 '15 at 16:05
  • and yes out of the getJSON method the variables are undefined, how can I get that variable? – Mattew Trincanati Feb 23 '15 at 16:06
  • put your code inside that checkDrink method you call from within the call back function. – Carlo Moretti Feb 23 '15 at 16:12
  • thx, still got some problems, question Updated! – Mattew Trincanati Feb 23 '15 at 16:26
  • ok, as I see it, you have the dataObject which is the correct object to put in the timeline_config you asked for. getJSON was the way to get it as a JSON (which is the topic of your question), putting it in the callback as suggested by @FelixKling was the way to being able to use it. The issue you're facing now with the blank page is probably related to the use you're making of that json, so it should really be a separate question – Carlo Moretti Feb 23 '15 at 16:31

0 Answers0