我正在學習Django和Angular。在本地運行的Angular服務器上運行的調用Django API
我已經安裝Django的API上http://serverip:8666/athletics/
我已經創建了我從我的本地機器上運行的小角度應用後端上。
在我的角度應用程序下面的代碼:
$scope.list_athletes = function(){
console.log('hey');
$http
.get('http://serverip:8666/athletics/')
.success(function (result) {
console.log('success');
})
}
生成錯誤:
XMLHttpRequest cannot load http://serverip:8666/athletics/ . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://127.0.0.1:65356 ' is therefore not allowed access.
是什麼原因導致這個錯誤?我如何解決它,以便我可以從我的本地Angular應用程序訪問Django API?
我應該在哪裏添加該行?它在Django應用程序中嗎?我在我的簡單Angular應用程序中只有一個響應。它是「返回響應(serializer.data)」 – user1283776
看起來像django-rest-framework的Response對象,所以你可以嘗試'返回響應(serializer.data,headers = {'Access-Control-Allow-Origin':'http: //127.0.0.1:65356/'}) –
工作,謝謝! (我刪除了http:之後的空格和端口號之後的空格。) – user1283776