Javascript http get请求在1中不起作用
我无法通过我的服务让URL工作,即使我可以让它在浏览器中工作 “$http()”似乎没有被调用。第一个警报()出现,但仅此而已 这是在浏览器中返回的URL= [{“id”:1,“name”:“denis”,“courses”:[{“id”:1,“name”:“mycourse”}]] 有人能就我做错了什么提出一些建议吗 studentform.js:Javascript http get请求在1中不起作用,javascript,angularjs,http,Javascript,Angularjs,Http,我无法通过我的服务让URL工作,即使我可以让它在浏览器中工作 “$http()”似乎没有被调用。第一个警报()出现,但仅此而已 这是在浏览器中返回的URL= [{“id”:1,“name”:“denis”,“courses”:[{“id”:1,“name”:“mycourse”}]] 有人能就我做错了什么提出一些建议吗 studentform.js: "use strict"; angular.module('StudentApp',[]); angular.module('Studen
"use strict";
angular.module('StudentApp',[]);
angular.module('StudentApp').service('StudentModel', [function($http){
this.students = [{name:"NONE"},{name:"NONE2"}];
this.getStudents = function(){
alert("getStudents called.");
$http({method: 'GET', url: 'http://localhost:8080/myapp/students'}).
then( function(data, status, headers, config){
alert("Success!!");
this.students = data;
}).
catch( function(data, status, headers, config){
alert("Failed!!");
console.log( "Error: " + status );
});
// $http.get('http://localhost:8080/myapp/students')
// .then( function (response) {
// this.students = response.data;
// }. bind( this), function (response) {
// console.log( response.data);
// });
// $http.get('http://localhost:8080/myapp/students')
// .then(function(response){
// alert( "in then");
// alert( response.date );
// this.students = response.data;
// }.bind(this),function(response){
// alert( "in bind" );
// console.log(response.data);
// });
// alert(response.data);
}
}]);
angular.module('StudentApp').controller('StudentController', ['StudentModel', function(StudentModel){
this.model = StudentModel;
}]);
studentform.jsp
<!DOCTYPE html>
<html ng-app="StudentApp">
<head>
<meta charset="UTF-8">
<script src="/myapp/resources/js/angular/angular.js"></script>
<script src="/myapp/resources/js/studentform.js"></script>
<title>Students</title>
</head>
<body ng-controller="StudentController as main">
<div>
<p><button ng-click="main.model.getStudents()">Get Students</button></p>
<ul>
<li ng-repeat="student in main.model.students">
{{student.name}}
</li>
</ul>
</div>
</body>
</html>
学生
吸引学生
-
{{student.name}
您忘记添加$http
作为依赖项
将您的服务定义更改为以下代码:
angular.module('StudentApp').service('StudentModel', ['$http', function($http){
您得到的错误是什么?你的浏览器开发工具显示了什么?我没有收到任何错误,这是问题的一部分。我有一些alert()调用,指向应该弹出的位置,但它们不会在成功或失败时弹出。我看到的只是“getStudents called”警报,然后什么都没有。我正在使用firefox和firebug。你能为这个问题创建一个plnkr吗?我刚刚在chrome中提出了这个问题,控制台说:TypeError:$http在fn的Object.getStudents(studentform.js:7)上不是函数(编译时eval(angular.js:15156),在Scope的callback(angular.js:26744)上不是函数。$eval(angular.js:17972)在范围中。在HTMLButtoneElement中应用$apply(angular.js:18072)。(angular.js:26749)在defaultHandlerWrapper(angular.js:3613)在HtmlButtoneElement.eventHandler(angular.js:3601)我不知道“plnkr”,后端是用java MVC spring/hibernate实现编写的。我将把代码签入GitHub。你可以在“谢谢”上看到,它让我克服了那个问题。非常感谢。