Hello everyone,
I’m trying to get dynamic data from a public WebService URL1 using get method in http, it works fine in browser but when i run it with “ionic serve” or directly on device, the get request is not processed and returns status -1.
Replacing the URL by another one in my code (for example URL2) without changing anything else works fine.
URL1 : https://openlibrary.org/api/books?bibkeys=ISBN:9782091841762&format=json&jscmd=data
URL2 : http://date.jsontest.com/
Here’s my Controller :
.controller(‘AppCtrl’, function($scope,$http) {
$scope.data = {isbn:’’};
$scope.result=’’;
// for debug
$scope.status = ‘’;
$scope.data = ‘’;
$scope.headers = ‘’;
$scope.config = ‘’;
$scope.showResponse = false;
$http.get('http://openlibrary.org/api/books?bibkeys=ISBN:9782091841762&format=json&jscmd=data')
.success(function(data, status, headers,config){
console.debug("http get success");
$scope.showResponse = true;
$scope.data = data;
$scope.status = status;
$scope.headers = headers;
$scope.config = config;
console.debug("Request finished with status "+status);
console.debug("URL : "+config.url);
console.debug("Method : "+config.method);
console.debug("Headers : "+config.headers);
console.debug("Data : "+data);
}).error(function(data, status, headers,config) {
console.debug("Error status : " + status);
//console.debug("headers : " + headers);
//console.debug("config : " + config);
$scope.showResponse = true;
$scope.data = data;
$scope.status = status;
$scope.headers = headers;
$scope.config = config;
console.debug("Request finished with status "+status);
console.debug("URL : "+config.url);
console.debug("Method : "+config.method);
console.debug("Headers : "+config.headers);
console.debug("Data : "+data);
});
})
Here is the result obtained with URL1 :
0 409636 debug Error status : -1
1 409637 debug Request finished with status -1
2 409637 debug URL : https://openlibrary.org/api/books?bibkeys=ISBN:9782091841762&format=json&jscmd=data
3 409638 debug Method : GET
4 409639 debug Headers : {“Accept”:“application/json, text/plain, /”}
5 409639 debug Data : null
Here is the result obtained with URL2
0 298747 debug http get success
1 298748 debug Request finished with status 200
2 298748 debug URL : http://date.jsontest.com/
3 298749 debug Method : GET
4 298750 debug Headers : {“Accept”:“application/json, text/plain, /”}
5 298751 debug Data : {“time”:“10:28:18 AM”,“milliseconds_since_epoch”:1465727298684,“date”:“06-12-2016”}
I have searched for hours now and don’t have the beginning of a clue, so thank you in advance for your help.