如何使用Cordova从url获取json数据?

时间:2017-03-21 14:49:50

标签: javascript android json cordova

我开始使用Cordova for android开发应用程序,我现在正在google搜索解决方案(Whitelist)以从URL获取JSON数据。但是我找不到简单的教程。我发现的大多数教程都不是那么初学者友好。我正在考虑尝试使用纯javascript获取JSON数据,但我认为这不是一个好主意。是否有一些简单的技巧或教程可以解决这个问题?我很乐意听到你的消息!

3 个答案:

答案 0 :(得分:1)

喜欢这个?假设hello.php返回您的JSON数据。

    $.ajax({
        url: "yourwebsite.com/hello.php",
        type: 'GET',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (arr) {
            _getdata(arr);
        },
        error: function () {
            validationMsg();
        }
    });

function _getdata(arr){
   //your JSON resuls are now in arr. Do what you need with the array.
}

答案 1 :(得分:1)

This example可能会非常有用。

你应该尝试ajax调用以从服务器获取数据,jQuery使它变得非常容易。以下是示例中用于从服务器加载数据的函数:

function getEmployeeList() {
$('#busy').show();
$.getJSON(serviceURL + 'getemployees.php', function(data) {
    $('#busy').hide();
    $('#employeeList li').remove();
    employees = data.items;
    $.each(employees, function(index, employee) {
        $('#employeeList').append('<li><a href="employeedetails.html?id=' + employee.id + '">' +
                '<img src="pics/' + employee.picture + '" class="list-icon"/>' +
                '<p class="line1">' + employee.firstName + ' ' + employee.lastName + '</p>' +
                '<p class="line2">' + employee.title + '</p>' +
                '<span class="bubble">' + employee.reportCount + '</span></a></li>');
    });
    setTimeout(function(){
        scroll.refresh();
    });
});

}

我希望它有所帮助。

答案 2 :(得分:0)

fetch('/echo/json', {
  method: 'get'
}).then((JSONresponse) => {
  // do whatever you want with your
  // JSONresponse here
})