1

I am trying to do a 'get' request through ajax on a url. everything works fine on the console but unable to get the raw json data to display on the page. here data.collections gives me an array of objects. I specifically want raw json data only on the page.

my script is this:

var research;
$.ajax({
        url: url,
        type: 'GET',
        dataType: 'json'})
.done(function(data){
research= JSON.stringfy(data.collections)
});

$('.rsh').html(research);

html is this <div class = 'rsh'></div>

I want raw json data on page like this but my page is blank.

{
  "lab": {
    "type": "xy",
    "year": "yz",
    "team": "yx"
  },
  "name": "qwerty",
  "assistants": 5,
}

edit: this question deals with displaying data in raw json format on the page. Unfortunately the problem in my code was wrong placement of the code. Initial question has nothing to do with asynchronous nature although it is good to know.

Ann
  • 139
  • 7
  • Possible duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) –  Nov 30 '15 at 06:10

1 Answers1

1
var research;
$.ajax({
       url: url,
       type: 'GET',
       dataType: 'json'})
    .done(function(data){
        research= JSON.stringfy(data.collections)
        $('.rsh').html(research);
});

An ajax call is asynchronous so you will need to use the response within the done/success method.

Rho
  • 370
  • 2
  • 10