Search code examples
htmlangularjshttpresponse

Why do I get null as response in angularjs http request


I am using angularjs as Front End and some Java Spring application as back end

The web service is RESTful Web Services

I should get JSON repose from server .

But the problem is when it is called it does not show anything in html page

$http({
    url: "http://melkban24.ir/city/json/2",
    dataType: "json",
    method: "GET",
    headers: {
        "Content-Type": "application/json"
    }
}).success(function(response){
    allStates = response;
       $log.info(response);
}).error(function(error){
    allStates = error;
       $log.info(error);
});

Every time it goes to error function


Solution

  • Cross Origin Request Sharing (CORS) is not enabled

    Error Screenshot Its an API issue. You are trying to request a resource which is not CORS enabled. Either try to make the call from same domain (i.e. host angular project on same domain as API) or enable CORS on the API Server.

    You can find the configuration to enable CORS for all the different web servers on the net.