jquery - use data from ajax call -


i using self host api , want use data ajax call in function, not work. undefinded.

my code:

function checkdata() { $.ajax({     type: "get",     url: "http://localhost:8080/api/data" }).success(function (result) {     var datareturned = result.statelog;     console.log('done' + datareturned);     x = datareturned;     test(x); }); }  setinterval(checkdata, 5000);  function test(data) { alert(data); // here undefined } 

do know how can use data?

thanks in advanced!

regards, trap

i try , work fine:

<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title></title>     <script src="js/jquery-1.8.1.min.js"></script>     <script>         function checkdata() {             $.ajax({                 type: "get",                 url: "file.data.txt"             }).success(function (result) {                 console.log('readed:' + json.stringify(result));                     var datareturned = json.parse(result).statelog;                 console.log('done ' + datareturned);                 var x = datareturned;                 test(x);             });         }          setinterval(checkdata, 5000);          function test(data) {             alert(data); // here undefined         }     </script>  </head> <body>  </body> </html> 

json data(file.data.txt):

{ "statelog":"active"  } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -