0

I'm trying to append the results from an ajax call to a paragraph using jQuery.

I would like to return the variable "myResult" from the inner getResult function and pass it to the outer buildParagraph function, but the value returned is undefined.

How do I append the value of myResults to the <p> tag as indicated below?

function buildParagraph () {

   function getResult(url) {  
      $.getJSON(url, function(data) {
         var myResult = data.results;
         return myResult;
      } 
   }

  var myUrl = 'www.mywebsite.com';

  getResult(myUrl);

  $('<p>').html(myResult);

}
mtaggart
  • 93
  • 5
  • 11
  • You can not return data from asynchronous methods..Use callbacks instead... – Rayon Feb 29 '16 at 04:24
  • Just move the instantiation of myResult into buildParagraph. As stated above no need to return in the response handler. – mrtig Feb 29 '16 at 04:27

1 Answers1

0

You need to have a callback function inside ajax success,Or the easiest way is move the below code to the ajax success function

$('<p>').html(data.results);
shu
  • 1,938
  • 10
  • 19