doulin2555 2014-08-04 23:29
浏览 159

从.php页面获取JSON数据并将其存储在全局变量中

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?

  • 写回答

1条回答 默认 最新

  • dsy6639 2014-08-05 00:01
    关注

    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);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料