Angularjs issue $http.get not working
angularjs, get, http, json, rest
Solution
This is because how angular treats CORS requests (Cross-site HTTP requests). Angular adds some extra HTTP headers by default which is why your are seeing `OPTIONS` request instead of `GET`. Try removing `X-Requested-With` HTTP header by adding this line of code:
delete $http.defaults.headers.common['X-Requested-With'];
Regarding CORS, following is mentioned on Mozilla Developer Network:
The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser.
Problem
I am a novice to Angularjs and tried to follow example given for $http.get on angularjs website documentation. I have a REST service, which when invoked returns data as follows: ``` http://abc.com:8080/Files/REST/v1/list?&filter=FILE { "files": [ { "filename": "a.json", "type": "json", "uploaded_ts": "20130321" }, { "filename": "b.xml", "type": "xml", "uploaded_ts": "20130321" } ], "num_files": 2} ``` Part of the contents of my index.html file looks like as follows: ``` <div class="span6" ng-controller="FetchCtrl"> <form class="form-horizontal"> <button class="btn btn-success btn-large" ng-click="fetch()">Search</button> </form> <h2>File Names</h2> <pre>http status code: {{status}}</pre> <div ng-repeat="file in data.files"> <pre>Filename: {{file.filename}}</pre> </div> ``` And my js file looks as follows: ``` function FetchCtrl($scope, $http, $templateCache) { $scope.method = 'GET'; $scope.url = 'http://abc.com:8080/Files/REST/v1/list?&filter=FILE'; $scope.fetch = function() { $scope.code = null; $scope.response = null; $http({method: $scope.method, url: $scope.url, cache: $templateCache}). success(function(data, status) { $scope.status = status; $scope.data = data; }). error(function(data, status) { $scope.data = data || "Request failed"; $scope.status = status; }); }; } ``` But when I run this, I do not see any result for filenames and I see http status code = 0 When I run , ``` http://abc.com:8080/Files/REST/v1/list?&filter=FILE ``` in browser, I still can see desired results (as mentioned above) I even tried to debug using Firebug in firefox, I see the above URL gets invoked when I hit "Search" button but response looks to be empty. And interestingly in Firebug under URL, it shows ``` OPTIONS "Above URL" ``` instead of ``` GET "Above URL" ``` Can you please let me know, what I am doing wrong and why I am not able to access JSON data ? Thanks,