简体   繁体   中英

How to get json data from url with Cordova?

I started to develop an app with Cordova for android and I'm now searching around google for a solution(Whitelist) to get the JSON data from the URL.But I cannot find a simple tutorial. Most of the tutorials I found are not so beginner friendly. I'm thinking about trying to get the JSON data with pure javascript, but I think it's not a good idea. Are there some simple tips or tutorial that can solve this problem? I love to hear from you!

Like this? Assuming that hello.php returns your JSON data.

    $.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.
}

This example could be very helpful.

You should try ajax calls in order to fetch data from the server, jQuery makes it very easy. Here is the function used in the example that loads the data from the server :

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();
    });
});

}

I hope it help.

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

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