0

I have a webapi in asp.net that gives me back JSON code. I want to access this with JQuery in a php-website.

I have this JQuery code to get the data from the webapi:

$.ajax({
    type: 'GET',
    url: 'localhost/webapi/api/data'
}).done(function (data) {        

});

How can I return the value of 'data' to a global variable? So that I have a list of objects where I can loop through with navigation buttons.

Example: When I click on the nextButton, I want to get the value of data[1].Text.

$('#nextButton').click(function() {
    data[1].Text;
});

1 Answers1

0

Perhaps like this:

var dataContainer = {}; // if you work with strings use ''

$.ajax({
    type: 'GET',
    url: 'localhost/webapi/api/data'
    }).done(function (data) {        
    dataContainer = data;
});

$('#nextButton').click(function() {
    if(dataContainer != {}){  // for strings != ''
       // use dataContainer
    }

});
Piotr Dajlido
  • 1,982
  • 15
  • 28
  • Please, mark it as useful or solved for others to see. – Piotr Dajlido Mar 23 '15 at 16:12
  • Put this in your console: `var dataContainer = {}; console.log(dataContainer != {});` What if the request is still going on when the button is clicked? This is a pretty terrible answer. – Adam Jenkins Mar 23 '15 at 16:15
  • Then the `if` condition is unsatisfied.. and nothing happens? why? – Piotr Dajlido Mar 23 '15 at 16:16
  • How can you do it then? – Grasshopper Mar 23 '15 at 16:17
  • Come on guys.. Author didn't ask for extended logic to append buttons to his page after the Ajax call. Maybe for his needs this solution is enough. Don't be so cruel, feel free to write your own extended logic that provides luxurious solution to this problem. – Piotr Dajlido Mar 23 '15 at 16:37