这里我无法在节点服务器
中打印我的控制台代码console.log("I got the data I requested");
浏览器控制台中显示的错误位于下面的图片中
这是server.js的代码
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"))
app.listen(3000);
console.log("server running on port 3000")
这是controller.js的代码
var app = angular.module('myApp', []);
app.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
$http({
method: 'GET',
url: '/employeelist'
}).then(function (response) {
console.log("I got the data I requested");
$scope.employeelist = response.data;
});
employee1 = {
name: 'Sunil',
designation: 'Software Developer',
salary: '20000'
};
employee2 = {
name: 'Vamshi',
designation: 'Java Developer',
salary: '30000'
};
employee3 = {
name: 'Chethan',
designation: 'Dot Net Developer',
salary: '20000'
};
var employeelist = [employee1, employee2, employee3];
$scope.employeelist = employeelist;
}]);
我尝试在节点服务器中打印控制台代码
我无法在此处打印我的console.log(“”)
答案 0 :(得分:4)
尝试捕捉错误
$http({
method: 'GET/POST',
url: 'your/endpoint/url'
})
.then(function onSuccess(response) {
console.log(response);
})
.catch(function onError(error) {
console.log(error);
});
如果您想全局取消警告
app.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}]);
但始终建议捕捉错误,而不是抑制警告
答案 1 :(得分:1)
我已经更改了server.js和controller.js代码,现在如果我打开localhost,我可以收到一个获取请求
这是我所做的改变
server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"));
app.get("/employeelist", function (req,res) {
console.log("I received a GET Request")
employee1 = {
name: 'Sunil',
designation: 'Software Developer',
salary: '20000'
};
employee2 = {
name: 'Vamshi',
designation: 'Java Developer',
salary: '30000'
};
employee3 = {
name: 'Chethan',
designation: 'Dot Net Developer',
salary: '20000'
};
var employeelist = [employee1, employee2, employee3];
res.json(employeelist);
});
app.listen(3000);
console.log("server running on port 3000");
controller.js
var app = angular.module('myApp', []);
app.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
$http({
method: 'GET',
url: '/employeelist'
}).then(function onSuccess(response) {
console.log("data recieved");
$scope.employeelist=response.data;
},function onError(error) {
console.log("Unknown error");
$scope.employeelist=error.data;
});
}]);
的index.html
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<title>Employees List</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>Employees List</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Designation</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employeelist">
<td>{{employee.name}}</td>
<td>{{employee.designation}}</td>
<td>{{employee.salary}}</td>
</tr>
</tbody>
</table>
</div>
<script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/
angular.min.js"></script>
<script src="../../controllers/controller.js"></script>
</body>
</html>