AngularJS: $HTTP.GET is getting cancelled -
i trying use $http service of angularjs doesn't seem work. have read online such issue solutions given doesn't help. have following in controller:
app.controller('ctrl', function($scope, $http){ var url = http://someurl?name=foo; $scope.submitrules = function(){ $http({method: 'get', url: url}). success(function(data, status) { $scope.status = status; $scope.data = data; }). error(function(data, status) { $scope.data = data || "request failed"; $scope.status = status; }); }; });
i using version of angularjs <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.min.js"></script>
whenever try sending request shows request status cancelled. url sending request working doesn't work form angularjs. can help?
i find cors stuff confusing. can say? i'm slow learner. problem describe may fixable controller, think it's better set things right @ point of entry. example:
angular.module('myapp', ['ngresource', 'ui.bootstrap']). config(function($httpprovider) { // enable cors $httpprovider.defaults.usexdomain = true; delete $httpprovider.defaults.headers.common['x-requested-with']; ... });
given code you've provided, may work (not tested):
app.controller('ctrl', function($scope, $http, $httpprovider){ var url = http://someurl?name=foo; $scope.submitrules = function(){ // enable cors $httpprovider.defaults.usexdomain = true; delete $httpprovider.defaults.headers.common['x-requested-with']; $http({method: 'get', url: url}). success(function(data, status) { $scope.status = status; $scope.data = data; }). error(function(data, status) { $scope.data = data || "request failed"; $scope.status = status; }); }; });
either way, magic happens in these 2 line:
$httpprovider.defaults.usexdomain = true; delete $httpprovider.defaults.headers.common['x-requested-with'];
Comments
Post a Comment