Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have just started with AngularJS and tried a very basic example. The webpage contains a button, on click of which I request a json from the server. But whenever, I click the button, the code executes the portion in the error function.

HTML file

<!DOCTYPE html>
<head>
    <title> step 4</title>
</head>

<body ng-app="step4App">
    <div ng-controller="FriendsCtrl">

        <button ng-click="loadFriends()">Load Friends</button>
    </div>

    <script src="angular.min.js"></script>
    <script src="xml2json.js"></script>
    <script src = "step4.js"></script>
</body>
</html>

JS

var app = angular.module("step4App",[]);

app.controller("FriendsCtrl", function($scope,$http) {
    $scope.loadFriends = function(){



        $http({method:'GET',url:'http://localhost:8080/test.jsp', params:{name:"abc",no:"LBF1151638"}}).success(function(data) {
            $scope.friends = data;
            document.write('<p>two</p>');
            document.write(data);
        }).error(function() {

            alert("error");
            });
            }

});

The jsp page as of now is returning this json response.

{"response":{"numFound":0,"start":0,"docs":[]}}

But I am not able to understand, why it always executes the error function.

I checked in the browser Console that a request is being made and a 47byte response is received (equal to the no of characters in the JSON). But it doesn't print the JSON.

share|improve this question

There could be something wrong happening on the server. Try opening the page with FireFox + Firebug and inspect the server response to make sure it's what you expect (check the status code and the Content-Type response header).

You can also run from a command line do a curl -v http://localhost:8080/test.jsp.

BTW Angular passes you extra parameters in the callbacks. Try alert-ing them too see what they are. From http://docs.angularjs.org/api/ng/service/$http:

$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
  // this callback will be called asynchronously
  // when the response is available
}).
error(function(data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});
share|improve this answer
    
thanks. I tried using firebug. It says status as 200(OK), but angularJS says 404. – krammer Feb 24 '14 at 5:45
    
You probably want to check the logic in you JSP file, something could be off. Or make sure you don't have a typo in your URL. – Christophe L Feb 24 '14 at 23:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.