2016-05-14 56 views
0

我有一個Django服務器amd我想用AngularJS發送一個請求給它。AngularJS發送請求x-www-form-urlencoded

這是我的代碼:

  var req = { 
       method: 'POST', 
       url: 'http://127.0.0.1:8000/api/user/register/', 
       headers: { 
        'Access-Control-Allow-Origin': '*', 
        'Content-Type': 'application/x-www-form-urlencoded' 
       }, 
       data: $.param({ phone_number: phonenumber, password: phonenumber, country: 1 }) 
      } 

      $http(req).then(function(response){ 
       callback(response); 
      }, function(response){ 
       console.log(response); 
      }); 

我收到以下錯誤:

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

我不知道有什麼可以在這裏是問題。我已經爲iOS和Android創建了一個應用程序,它們都與同一臺服務器進行通信,而且我沒有任何問題。

我使用AngularJS 1.5.5 https://code.angularjs.org/1.5.5/

有人能提出什麼是錯在AngularJS這個要求嗎?

回答

0

看來,代碼並不需要這一部分:

'Access-Control-Allow-Origin': '*',

真正的問題是,因爲服務器不允許本地URL。