T
T
tupoi2016-10-15 19:29:55
Angular
tupoi, 2016-10-15 19:29:55

How to make a GET request in angularjs?

Good day, when trying to make a request, AngularJS gives an error like

XMLHttpRequest cannot load http://127.0.0.1:8000/api/cat/2/. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:63342' is therefore not allowed access.

The backend is written in Django using the Django Rest Framework, for training I decided to make a simple model with several parameters, then I switched to Frontend and then an incomprehensible error, I use python tried to access the server, everything works out, the answer comes, but something went wrong with Angular .
Angular Application Code
function MainCtrl ($scope, $http, $log) {
  $http({
      method: 'GET',
      url:"http://127.0.0.1:8000/api/cat/2/"
  })
      .success(function (response) {
      $scope.myResp = response.data;
          $log.info(response);
  });
}


angular
    .module("app", [])
    .controller("MainCtrl", MainCtrl);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2016-10-17
@maximkv25

Set cors for django
You send requests to different ports

M
mortyy, 2016-10-16
@mortyy

You can try $http.jsonp. Cross-domain requests with Ajax are not so easy to send.

N
nuclear_kote, 2016-10-16
@nuclear_kote

It is necessary to set the allow-origin:* header on the server. Google cors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question