Vaibhav Jain
Vaibhav Jain

Reputation: 3755

AngularJS REST Call Error

In my project I have one json files, One I deployed in the Local Machine/Server and other I deployed in the another server.

Here is my code for calling the json data from angular controller -

function Controller($scope, $http) {
    $http.get("http://localhost:8080/RESTCall/test.json").
        success(function(data) {
            $scope.lists= data;
        });
}


function Controller2($scope, $http) {
    $http.get("http://<Server IP>:8080/RESTCall/test.json").
        success(function(data) {
            $scope.lists= data;
        });
}

Here is my index.html page, where I am using both this controller :

    <div ng-controller="Controller">    
        <ul ng-repeat="list in lists">
            <li>            
            {{list.testdata}}
            </li>
        </ul>     
    </div>


    <div ng-controller="Controller2">    
        <ul ng-repeat="list in lists">
            <li>            
            {{list.testdata}}
            </li>
        </ul>     
    </div>

While executing I am getting the data which is coming from the Local Machine/Server OR but for Controller1 I am getting following Error :

OPTIONS http://<SERVER IP>:8080/RESTCall/test.json net::ERR_CONNECTION_REFUSED

When I am accessing direct http://<SERVER IP>:8080/RESTCall/test.json I am able to get the data..

Upvotes: 0

Views: 161

Answers (1)

nhd
nhd

Reputation: 141

You are firing a cross-domain request (CORS). The browser will fire a pre-flight OPTIONS request first before firing the actual GET request. Therefore you will have to configure your server to response to OPTIONS request and return appropriate Access-Control-* headers.

Upvotes: 1

Related Questions