I am creating an AngularJS client and connecting it to a REST API I have running on NodeJS. I do GET requests to get the data by going to the URL and then using the stock symbol i.e. localhost:8080/stocks/aapl for Apple's share data. In AngularJS I am using the $http service and then doing data-binding like this:
var restURL = "http://localhost:8080/stocks/" + searchSymbol;
$http({
method: 'GET',
url: restURL
}).then(function successCallback(response) {
//Variables that are going to be used
var data, symbol, companyName;
//Parsing the data
data = angular.fromJson(response);
//Putting the data into the variables
symbol = data.symbol;
companyName = data.name;
//Putting data from variables into Angular code
$scope.companyName = companyName;
$scope.symbol = symbol;
}, function errorCallback(response) {
var message = "Error getting response from server";
$scope.errorMessage = message;
});
and this is what the view component looks like:
<h1>Search Stocks</h1>
<p>Search for stocks using the text box below</p>
<p><input type="text" ng-model="searchSymbol" placeholder="Search by Symbol"></p>
<p><button ng-click="search()">Search</button></p>
<p> {{ symbol }}</p>
<p>{{ companyName }}</p>
<p>{{ errorMessage }}</p>
When I click the button, the GET request is reflected on the NodeJS server as it shows the company data in the terminal view however I can't seem to get it parsed in Angular.
data = angular.fromJson(response);. What is the value of the object?responseand check what it is. Btw, you can just write$http.get(restUrl).then(....