1

I have a piece of Code in angularjs. If I hard code the value of http response it is displaying the response when I use the http method in angularjs it is not displaying. But I am getting response to that link locally. I dont know where I am wrong. Here is the code

<!DOCTYPE html>
<html>
 <head>
 <script script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
 <script>
 angular.module('myApp', [])
 .controller('MyCtrl', function($scope,$http){
 $scope.test = [
 {
 "done": 2,
 "total": 7
 }
];
});
 </script>
 </head>
 <body ng-app="myApp" ng-controller="MyCtrl">
 <div ng-repeat="t in test">
 <span>{{t.done}}</span>
 <span>{{t.total}}</span>
 </div>
 </body>
</html>

In Script again if i add the below code,its not displaying the values

<script>
 angular.module('myApp', [])
 .controller('MyCtrl', function($scope,$http){
 $http.get("http://localhost:8080/reports/webapi/hello/myresource2").then(function (response) {
 $scope.test = response;
 });
});
 </script>
Marcus Höglund
16.9k11 gold badges53 silver badges72 bronze badges
asked Jul 8, 2016 at 6:42
8
  • 4
    Your code is confusing. You are doing ng-repeat on test variable. In your MyCtrl, there is no such variable? Commented Jul 8, 2016 at 6:46
  • Oops sorry my mistake...its $scope.test only Commented Jul 8, 2016 at 6:53
  • please check console.log(response) ? Commented Jul 8, 2016 at 6:56
  • which kind of response this localhost:8080/reports/webapi/hello/myresource2 link is sending. Commented Jul 8, 2016 at 7:06
  • @Teja Please use jsfiddle,jsbin... to simulate the scenario Commented Jul 8, 2016 at 7:14

4 Answers 4

1

Try disabling web security of google chrome if you are running google chrome and run your application it will work normally.


Your problem is not with $http rather with CORS. I've ran into this issue when I was learning angularJS.

Since AngularJS or new frameworks using AJAX extensively there is a small issue called CORS(Cross origin resource sharing) which means we can access a resource from the same domain due to security concerns.

Please refer Understanding CORS.

Web applications are served by servers however you've tried to open your html directly in browser which means you are trying to access a http resource from file protocol which is not allowed as per CORS.

You've two options to use.

  1. Disable web security in google chrome
  2. Run your application in a static server.
answered Jul 8, 2016 at 7:31
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much......It worked.......just implemented second option
In addition to that, it should be response.data not just the response
0

Your actual data resides in response.data. So update your scope variable accordingly,

Your controller code,

angular.module('myApp', [])
 .controller('MyCtrl', function($scope, $http) {
 $http.get("http://localhost:8080/reports/webapi/hello/myresource2")
 .then(function (response) {
 $scope.test = response.data;
 });
});

Hope this solves the issue.

answered Jul 8, 2016 at 6:47

10 Comments

Can you add console.log(response.data) and post the output?
angular.module('myApp', []) .controller('MyCtrl', function($scope,$http){ $http.get("localhost:8080/reports/webapi/hello/myresource2") .then(function (response) { $scope.test = response.data; console.log(JSON.stringify($scope.test)); });............Its Blank
No console output?
Are you running your application as a web application or by simply opening index.html in browser?
First you should disable web security of google chrome and run you abc.htm since you are trying to access http protocol from file protocol.
|
0

try to add another function for the catching of error response

$http.get("http://localhost:8080/reports/webapi/hello/myresource2").then(function (response) {
 $scope.test = response;
 }, function(response){
 console.log(response)
 });
answered Jul 8, 2016 at 7:04

Comments

0

I also went to the similar problem and solved by adding the following HTTP headers at the response of the receiving end.

Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: 

You may prefer not to use the * at the end, but only the domainname of the host sending the data. Like *.example.com

But this is only feasible when you have access to the configuration of the server.

You need to add these headers in the server, not in AngularJS Since you are sending json data through your api .You have to do like this.

<script>
 angular.module('myApp', [])
 .controller('MyCtrl', function($scope,$http){
 $http.get("http://localhost:8080/reports/webapi/hello/myresource2").then(function (response) {
 $scope.test = response.data;
}); 
 });
</script>
answered Jul 8, 2016 at 7:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.