0

I am trying to make a GET call to test a REST API but it keeps returning null, is there anything I am doing wrong:

Making the call in controller.js

function ConsumerSearchCtrl($scope, BusinessCategoryService) {

    console.log(BusinessCategoryService);

}

enter image description here

127.0.0.1:8000/api/category/ works perfectly fine

enter image description here

Code in services.js for API

/**
 *
 */
function BusinessCategoryService(WenzeyAPI, $q) {

    var scope = this;

    scope.categories = categories;

    function categories() {
        var q = $q.defer();
        WenzeyAPI.get('http://127.0.0.1:8000/api/category/').then(function success (res) {
            q.resolve(res.data);
        }, function failure (err) {
            q.reject(err);
        })
        return q.promise;

    }

}



/**
 *
 */
function WenzeyAPI() {

    var scope = this,
        ip = "http://127.0.0.1:8000";

    scope.get = get;
    scope.post = post;

    function get(url, data) {

        data = data || {};

        var req = {
            method: 'GET',
            url: url,
            data: data
        }

        var q = $q.defer();
        $http(req).then(function success(response) {
            q.resolve(response);
        }, function failure(err) {
            q.reject(err);
        });
        return q.promise;
    }

    function post(url, data) {

        data = data || {};

        var req = {
            method: 'POST',
            url: url,
            data: data
        }

        var q = $q.defer();
        $http(req).then(function success(response) {
            q.resolve(response);
        }, function failure(err) {
            q.reject(err);
        });
        return q.promise;
    }

} 
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • with the browser dev tools, is there any error message in the console tab? anything interesting in the network tab (corresponding to the GET request)? you mentioned that it worked against localhost. what's the URL that's failing? – jdigital May 30 '16 at 23:28
  • It works when I access it in localhost, but it doesn't work from the Javascript application – methuselah May 30 '16 at 23:36
  • There are no error message in browser dev tools – methuselah May 30 '16 at 23:36
  • What does `doesn't work from the Javascript application` mean? What exactly is returning `null` – charlietfl May 30 '16 at 23:40
  • Whenever I make the call in controller.js – methuselah May 30 '16 at 23:41
  • 3
    Note that using `$q.promise` is anti-pattern when `$http` already returns promise https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns – charlietfl May 30 '16 at 23:42
  • That's not very defintitive. Is `BusinessCategoryService` an angular factory or service – charlietfl May 30 '16 at 23:43
  • @charlietfl `BusinessCategoryService` is a service – methuselah May 30 '16 at 23:48
  • Check the network tab to see if the request is dispatched from the browser. Also check the console for any errors. – Saneesh B May 31 '16 at 04:51
  • @SaneeshB just checked, seems the request isn't dispatched from the browser. No errors as well – methuselah May 31 '16 at 06:49
  • Add a breakpoint in WenzeyAPI.get . does it reach at that point ? – Saneesh B May 31 '16 at 07:16
  • You not calling `BusinessCategoryService.categories` in your example code which is the bit that actually returns the promise for your network access. You really should just return the promise from `$http` instead of making your own, finally you really should refrain from using the variable `scope` to describe your context, services/factories cannot inject `$scope` it could confuse future developers. – ste2425 May 31 '16 at 07:36
  • Is the JavaScript running from the same host and port? – Dave Van den Eynde May 31 '16 at 07:57
  • @ste2425 how should the code look like – methuselah May 31 '16 at 08:35
  • @DaveVandenEynde for Javascript, http://localhost:8100/... for API http://127.0.0.1:8000/ – methuselah May 31 '16 at 08:36
  • @methuselah yup, your problem is known as "same-origin policy". Your browser will not let your JS do XmlHttpRequest to another origin (domain + port) because of security implications. – Dave Van den Eynde May 31 '16 at 08:59
  • @DaveVandenEynde tested it with this: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en, still no error message – methuselah May 31 '16 at 09:02
  • @methuselah yeah, still, I'm convinced that this is the problem. – Dave Van den Eynde May 31 '16 at 09:05
  • Possible duplicate of [XMLHttpRequest cannot load https://www.\[website\].com/](http://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-https-www-website-com) – Dave Van den Eynde May 31 '16 at 09:06

1 Answers1

0

Removing WenzeyAPI and using $http resolved it.

function BusinessCategoryService($http) {

    this.getAllData = function () {

        return $http({
            method: 'GET',
            url: 'http://127.0.0.1:8000/api/category/',
        });

    }

}
methuselah
  • 12,766
  • 47
  • 165
  • 315