java - Fetching data from local array of values using angular not working -
i tried fetch data mysql, h2, derby databases nothing worked. tried given on w2schools website. later thought try array function , check java jsp page in angularjs page calling it.
below index.html:
<!doctype html> <html> <head> <meta charset="utf-8"/> <style> table, th , td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } table tr:nth-child(odd) { background-color: #f1f1f1; } table tr:nth-child(even) { background-color: #ffffff; } </style> <script src="angular.min.js"></script> </head> <body> <div ng-app="myapp" ng-controller="customersctrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.name }}</td> </tr> </table> </div> <script> var app = angular.module('myapp', []); app.controller('customersctrl', function($scope, $http) { $http.get("http://localhost:8080/pavanjsp/jsondata.jsp") .then(function (response) {$scope.names = response.data.records;}); }); </script> </body> </html>
here jsondata.jsp below:
<%@ page contenttype="text/html; charset=iso-8859-1" language="java" %> <html> <head> <title>json data</title> <link rel="stylesheet" href="dist/css/bootstrap.css" /> </head> <body> <div class="container-fluid"> <% string[] names = { "alfreds futterkiste", "b's beverages", "berglunds snabbköp", "blondel père et fils", "bólido comidas preparadas", "bon app'", "comércio mineiro" }; response.addheader("access-control-allow-origin", "*"); %> <% out.print("{ \"records\":[ "); for(int = 0; < names.length; i++) { if(i == (names.length - 1)) out.print("{\"name\":\"" + names[i] + "\"}"); else out.print("{\"name\":\"" + names[i] + "\"}, "); } out.print(" ] }"); %> </div> </body> </html>
i tried putting index.html index.jsp , deploying in tomcat. doesn't works. php code have tried not working.
please me out.
try changing code to:
$scope.names = json.parse(response.data).records;
if doesn't work, debug if receive proper json server. open firebug/developer tools ( f12) , see network.
open page , see response. copy , paste json validation tool. if json proper, wrong in angular app, see fine.
also check in firebug call, if there cors configured. if receive result 200 ok, it's fine.
also can use $log service debug result:
app.controller('customersctrl', function($scope, $http,$log) .then(function (response) {$log.debug('response',response);
i hope helps.
Comments
Post a Comment