我正在編寫我的第一個AngularJS應用程序,並在Angular中遇到CORS問題。嘗試了這個reply中提到的內容,但仍然沒有解決這個問題的運氣。無法使用Angularjs執行CORS請求
的JS代碼(main.js)如下:
var app = angular.module("app",[]);
app.config(['$httpProvider', function($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);
app.controller("AppCtrl", function ($scope, $http) {
var postData = "username=demo&password=demo";
$http.post("http://sampleurl.com/login",postData).success(function(data, status, headers, config)
{
console.log(data);
});
});
index.html文件是:
<!DOCTYPE html>
<html>
<head>
<title>Sample Angular App</title>
</head>
<body ng-app="app" ng-controller="AppCtrl as app">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<script src="main.js"></script>
</body>
</html>
我得到的錯誤是:
Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
我已經嘗試過使用Ajax來運行這個請求,並且它在Ajax中工作得很好。出於某種原因,無法使用Angular獲得此功能。任何想法,我在這裏做錯了嗎?
謝謝!
男人,你讓我的日子變得如此之美!我在這個問題上苦苦掙扎!!!謝謝,謝謝! (PS:來自Angular的一些「角度」,我不太喜歡:-D) –