1

How request a JSON with ajax on AngularJS? I have written my site on django. On get request to 0.0.0.0:8000/json/ ,it sends back a json.object

At django, part views.py:

def send_json(request, param):
        result_dict = dict()
        result_dict['hello'] = 'world'
        return HttpResponse(json.dumps(result_dict, ensure_ascii=False), content_type="application/json; encoding=utf-8")

When I visit 0.0.0.0:8000/json/ in browser, I see json.

On Angular, I have read this article - fdietz.github.io/recipes-with-angular-js//consuming-external-services/requesting-json-data-with-ajax.html and implemented my code

(function(){
    angular.module('myapp',[])

    //define controller and inject $http service as dependency.
    .controller('ajax',['$http','$scope',function($http,$scope){ 
        $http.get('0.0.0.0:8000/json/').success(function(data, status, headers, config) {
                    $scope.posts = data;
                }).
                    error(function(data, status, headers, config) {
                });
    }])        
})();

GET request in Django is working but nothing is being displayed. When I change link at angular to fetch a local json it's working perfectly.

What is the mistake I am making?

5
  • 0.0.0.0 isn't an address you can request; it's just the address you can tell your server to listen on as it means "bind to all IPs". You should use "localhost" or "127.0.0.1". Commented May 6, 2016 at 8:25
  • "localhost" or "127.0.0.1" doesn't help. AngularJS catch error (I append alert(1) at error and see it :) ) Commented May 6, 2016 at 8:32
  • @Nuvemap is yout call going though. Can you put a console.log($scope.posts) when the $http call is being successful and check if the http is successful is getting the data. Commented May 6, 2016 at 8:36
  • 1
    Do you mind telling us what the error is, then? Commented May 6, 2016 at 8:37
  • It was Access-Control-Allow-Origin error :( I fix it. Commented May 6, 2016 at 10:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.