简体   繁体   中英

Taking JSON data from a .php page and storing it in a global variable

I am working on a cordova mobile application and I want to take all the data from a php page which displays the data in JSON and stores it in a global variable for local access.

here my JSON data:

{"success":1,"message":"Details Available!","details":[{"ID":"4","cohort_name":"Stuart Little","pin":"53870","start_date":"2014-08-02"},{"ID":"5","cohort_name":"Lexi Belle","pin":"19224","start_date":"2014-08-04"},{"ID":"6","cohort_name":"Joe Bloggs","pin":"12345","start_date":"2014-08-04"}]}

The method of which I am going to get this data is as follows:

var json = (function () {
var json = null;
$.ajax({
    'async': false,
    'global': false,
    'url': URL goes here,
    'dataType': "json",
    'success': function (data) {
        json = data;
    }
});
return json;
})();

The problem is that I am borrowing a web server to run the backend where I pull the data from and I only have an IP address to route the ajax request to the page. Is there any other way for me to get the data and store it? Or how do I use an IP address in this kind of request?

If you only request JSON data via a GET request, this shortcut might work, too:

$.getJSON("http://127.0.0.1/script.php?output=json", function(json) {
  console.log("JSON Data:" + json);
});

jQuery.getJSON


Alternative:

var url = 'http://127.0.0.1/script.php?output=json';
return $.ajax({
    type: "GET",
    url: url,
}).done(function (data) {
    console.log("JSON Data:" + data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM