0

I'm trying to consume webservice on my local machine and use AngularJS $http. Via browser everything works fine and I receive correct JSON response. But with AngularJS $http I receive 404 error, however, handler is found and response is being generated.

FrontEnd:

$http.get('http://localhost:8080/apartments.json').
            success(function(data){
                $scope.apartments = data;
            }).
            error(function(data, status){
                alert("Error "+ status);
            });

BackEnd:

@Controller
public class ApartmentsController {

    @RequestMapping(value = "/apartments", method = RequestMethod.GET)
    public @ResponseBody List<ApartmentEntity> getAll(){
        ApartmentEntity apartmentEntity = new ApartmentEntity();
        apartmentEntity.setName("test");
        apartmentEntity.setPrice(1000);
        return Arrays.asList(apartmentEntity);
    }
}

Servlet is mapped correctly.

Could you please suggest what is wrong?

6
  • Where are you serving the angular html file from? Commented Feb 19, 2014 at 16:42
  • It's separate web-server on the same machine. Commented Feb 19, 2014 at 16:51
  • The vital question here is if your RESTful endpoint is being served under the same origin as your client application. If that is the case, then we can delve into why the endpoint is not being hit. Otherwise, we may need to talk about CORS or proxying strategies. Commented Feb 19, 2014 at 17:15
  • In fact, Websevice is being hit from browser and from client side as well. I suspected the issue with cross domain request, but i don't receive any message regarding it. Only 404 error. Commented Feb 19, 2014 at 17:23
  • @shevchik I think you are not getting my question. Can you tell me the protocol, hostname and port for your RESTful server, and the same for the one serving your application? For instance, RESTful server is: http://localhost:8080 and Web application is being reached from http://localhost:4040. Then you have a CORS conflict. Commented Feb 19, 2014 at 17:30

1 Answer 1

1

The problem is that Angular is by default trying to use CORS as this is an ajax request attempt from one page loaded from one server to a REST service running in another server.

The way it tries to use CORS is by issuing the GET with a default header that triggers the browser to do a 'pre-flight' OPTIONSrequest.

This pre-flight automatic OPTIONS request is for the browser to get the list of supported methods by the server, and this is the first step of CORS.

So the 404 comes from this OPTIONS request.

See this issue in Angular, it seems that this was fixed in more recent versions as it was causing a lot of questions from users. See also the original issue that triggered the fix.

In you case for local development you can always deploy both applications in the same server and the problem is solved.

If in production it's necessary to run frontend/backend on separate servers, then either proxy those servers by a common frontend server like nginx/Apache or or turn on CORS, see enable-cors.org.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for explanation where does 404 error come from
@shevchik An alternative approach to enabling CORS (which may not be always possible [not speaking technically]) is to proxy your requests. That is, all RESTful endpoint requests, can be forwarded by you own web application server to the appropriate RESTful endpoint server. Since the server-side is not subject to CORS.

Your Answer

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.