[英]http get request is not working in angular 1
I am unable to get the URL to work via my service even though I can get it to work in the browser. 即使可以在浏览器中使用该URL,我也无法通过该服务使用该URL。
The "$http()" doesn't seem to get invoked. “ $ http()”似乎没有被调用。 The first alert() comes up, but that is it.
出现第一个alert(),仅此而已。
This is what come back in the browser for the URL = http://localhost:8080/myapp/students 这是在浏览器中返回的URL = http:// localhost:8080 / myapp / students
[{"id":1,"name":"denis","courses":[{"id":1,"name":"mycourse"}]}] [{ “ID”:1, “名称”: “丹尼斯”, “课程”:[{ “ID”:1, “名称”: “mycourse”}]}]
Can someone make some suggestions as to what I am doing wrong? 有人可以对我在做什么做些建议吗?
studentform.js: studentform.js:
"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 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>
You forgot to add $http
as a dependency 您忘记添加
$http
作为依赖项
Change your service definition to below code: 将服务定义更改为以下代码:
angular.module('StudentApp').service('StudentModel', ['$http', function($http){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.