How to get json data from php RESTAPI


#1

I have an electron windows application, in that I need to fetch a json object from external url (PHP RESTAPI) and store in a variable and manipulate it to display the result.

I have tried http.get, request and fetch method but I am not getting the desired result (JSON Object) either it is coming as empty or html page content. But when the url is entered in browser it is working perfect and hence API is working perfect. I don’t know where I am wrong.

Methods I have tried. (I have used alert to check the output)

**1. request **
var url =“http://192.168.21.50:90/tracker/index.php/user/ajax_get_users”;

const request = require('request');

request(url, { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
alert(body.url);
alert(body.explanation);
});

2. http

var url =“http://192.168.21.50:90/tracker/index.php/user/ajax_get_users”;

http.get(url, function(res){
    var body = '';

    res.on('data', function(chunk){
        body += chunk;
    });

    res.on('end', function(){
        var fbResponse = JSON.parse(body);
        alert("Got a response: ", fbResponse.picture);
    });
}).on('error', function(e){
      alert("Got an error: ", e);
});**strong text**

3.fetch
var url =“http://192.168.21.50:90/tracker/index.php/user/ajax_get_users”;

fetch(url).then(function(response) {
    alert(response);
  });

I have tried all this methods with no success in it.
Kindly suggest the appropriate way to achieve my goal.