Java 获取 angular.js 文件 net::ERR_ABORTED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46802053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GET angular.js files net::ERR_ABORTED
提问by LogronJ
I need your help to find out where's my error. I am using springboot(backend) x Angularjs(frontend). I get a GET (angular.js) files net::ERR_ABORTED when trying to load localhost:8080. I don't find any mistake on my angularfiles either. So here is my codes:
我需要你的帮助来找出我的错误在哪里。我正在使用 springboot(后端)x Angularjs(前端)。尝试加载 localhost:8080 时,我得到了一个 GET (angular.js) 文件 net::ERR_ABORTED。我的 angularfiles 也没有发现任何错误。所以这是我的代码:
//for the springcontroller
@RestController
public class EmployeeController {
@Autowired
EmployeeService es;
@RequestMapping(method=RequestMethod.GET,value="/employees")
public List<Employee> getAllEmployees(){
return es.getAllEmployees();
}
@RequestMapping(method=RequestMethod.GET,value="/employees/{name}")
public Employee getEmployee(@PathVariable String name){
return es.getEmployee(name);
}
@RequestMapping(method=RequestMethod.POST,value="/employees")
public void addEmployee(@RequestBody Employee emp){
es.addEmployee(emp);
}
@RequestMapping(method=RequestMethod.PUT,value="/employees/{name}")
public void updateEmployee(@RequestBody Employee emp,@PathVariable String name){
es.updateEmployee(emp,name);
}
@RequestMapping(method=RequestMethod.DELETE,value="/employees/{name}")
public void deleteEmployee(@PathVariable String name){
es.deleteEmployee(name);
}
}
For my app.js:
对于我的 app.js:
(function(){
'use strict';
var app = angular.module('myApp',['ngRoute','myApp.controller']);
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'index.html',
controller: 'HomeController'
});
});
})();
For my HomeController.js
对于我的 HomeController.js
var module = angular.module('myApp.controller',['myApp.service']);
module.controller('HomeController',['$scope','HomeService',
function($scope,HomeService){
$scope.EmpData = [];
var onSuccess = function(response){
$scope.EmpData = response.data;
};
var onError = function(error){
console.log('Error fetching data...');
};
function getAllEmployees(){
HomeService.getAllEmployees().then(onSuccess,onError)
};
} //end of function
]);
For my HomeService.js
对于我的 HomeService.js
var module = angular.module('myApp.service',[]);
module.service('HomeService',['$http',function($http){
function doFetchEmployees(){
var response = $http.get("/employees");
return response;
};
return{
getAllEmployees : function(){
return doFetchEmployees();
}
};
}]);
And finally for my index.html
最后是我的 index.html
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="src/main/resources/scripts/angular.min.js"></script>
<script src ="src/main/resources/scripts/angular-route.min.js"></script>
<script src="src/main/resources/app.js"></script>
<script src = "src/main/resources/HomeController.js"></script>
<script src ="src/main/resources/HomeService.js" ></script>
</head>
<body ng-app="myApp" ng-controller="HomeController">
<h2>Home</h2>
<br/>
<a href="#/index.html">Home</a>
<br/>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>telephone</th>
</tr>
</thead>
<tbody ng-repeat="emp in EmpData">
<tr>
<td>{{emp.name}}</td>
<td>{{emp.email}}</td>
<td>{{emp.address}}</td>
<td>{{emp.telephone}}</td>
</tr>
</tbody>
</table>
<br/>
<a href="http://localhost:8080/adduser.html">Create User</a>
</body>
</html>
And here are all my error message:
这是我所有的错误信息:
GET http://localhost:8080/src/main/resources/scripts/angular-route.min.js net::ERR_ABORTED
localhost/:7 GET http://localhost:8080/src/main/resources/app.js net::ERR_ABORTED
localhost/:5 GET http://localhost:8080/src/main/resources/scripts/angular.min.js net::ERR_ABORTED
localhost/:8 GET http://localhost:8080/src/main/resources/HomeController.js net::ERR_ABORTED
localhost/:9 GET http://localhost:8080/src/main/resources/HomeService.js net::ERR_ABORTED
localhost/:6 GET http://localhost:8080/src/main/resources/scripts/angular-route.min.js net::ERR_ABORTED
localhost/:7 GET http://localhost:8080/src/main/resources/app.js net::ERR_ABORTED
localhost/:8 GET http://localhost:8080/src/main/resources/HomeController.js net::ERR_ABORTED
localhost/:9 GET http://localhost:8080/src/main/resources/HomeService.js net::ERR_ABORTED
My index.html has no output just {{emp.names}} -> like this. I already tried putting the script tags inside the body but still the same. Hard cache reload & clear. still no luck. And Lastly when i try access localhost:8080 my eclipse console logs shows these 3 lines, is this also some cause of the error?
我的 index.html 没有输出,只是 {{emp.names}} -> 像这样。我已经尝试将脚本标签放在正文中,但仍然相同。硬缓存重新加载和清除。仍然没有运气。最后,当我尝试访问 localhost:8080 时,我的 Eclipse 控制台日志显示了这 3 行,这也是导致错误的原因吗?
2017-10-18 10:31:22.115 INFO 9176 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-10-18 10:31:22.115 INFO 9176 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-10-18 10:31:22.127 INFO 9176 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 12 ms
I'm having a hard time trying to figure this out.
我很难弄清楚这一点。
采纳答案by LogronJ
The problem was the full source path for the angular scripts.
问题是角度脚本的完整源路径。
I've changed it out into like this:
我把它改成了这样:
<script src="angular.js""></script>
<script src ="angular-route.min.js"></script>
<script src="app.js"></script>
<script src = "HomeController.js"></script>
<script src ="HomeService.js" ></script>
回答by Bhushan Dafale
try to add following line in your spring-mvc.xml (may be diff. name in your project), and change your location folder name (if its not resources, in my condition its resources)
尝试在您的 spring-mvc.xml 中添加以下行(可能是您项目中的差异名称),并更改您的位置文件夹名称(如果它不是资源,在我的情况下是它的资源)
spring-mvc.xml
spring-mvc.xml
<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
*.jsp
*.jsp
<script type="text/javascript" src="${ pageContext.request.contextPath }/resources/script/angular.js"></script>
if already added, please check that you've added cache-period
如果已经添加,请检查您是否添加了缓存周期
回答by MFarooqi
I had the same issue. But I thought.. if the same website is working on the other server why isn't on my server.. So.. what i did..
我遇到过同样的问题。但我想..如果同一个网站在另一台服务器上工作,为什么不在我的服务器上..所以..我做了什么..
I tried to directly access to that page. using URL..
我试图直接访问该页面。使用网址..
https://localhost:8080/folder/folder/script.js
https://localhost:8080/folder/folder/script.js
https://localhost:8080/folder/folder/style.css
https://localhost:8080/folder/folder/style.css
Every time i was unable to access those files.
每次我都无法访问这些文件。
Then I checked folder permission .. that was 700 so i changed that to 755..
然后我检查了文件夹权限 .. 那是 700 所以我把它改成了 755 ..
now. everything is running great :)
现在。一切都运行良好:)
回答by Dhiraj Belure
<script src="/src/main/resources/scripts/angular.min.js"></script>
<script src ="/src/main/resources/scripts/angular-route.min.js"></script>
<script src="/src/main/resources/app.js"></script>
<script src = "/src/main/resources/HomeController.js"></script>
<script src ="/src/main/resources/HomeService.js" ></script>
You need forward slash before src attribute in a script to remove "net:: ERR_ABORTED"
您需要在脚本中的 src 属性之前使用正斜杠来删除“net:: ERR_ABORTED”