本文介绍了404错误在AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来AngularJS。我startted与。在tutorail $ http.get 方法被调用JSON文件。在我的情况,我总是得到404错误。 JSON文件的位置也是相对的HTML文件。但得到了同样的错误总是如此。

Hi I am new to AngularJS. I startted with this tutorial. In tutorail $http.get method is invoking a JSON file. In my case I always getting 404 Error. Location of JSON file is also relative with HTML file. But getting the same error always.

下面是我的code:
index.html的:

Here is my code:index.html:

<!doctype html>
<html lang="en" ng-app>
<head>
<meta charset="utf-8">
  <title>My HTML File</title>
  <script src="angular.js"></script>
  <script src="controllers.js"></script>
</head>
<body ng-controller="PhoneListCtrl">
 Total Number of phones: {{phones.length}}
 Search: <input ng-model="query">
 Sort by:
<select ng-model="orderProp">
  <option value="name">Alphabetical</option>
  <option value="age">Newest</option>
</select>
<div>
  <ul>
    <li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
  {{phone.name}}
  <p>{{phone.snippet}}</p>
    </li>
  </ul>
</div>

</body>
</html>

controllers.js

controllers.js

function PhoneListCtrl($scope, $http) {
$http.get('phones.json').success(function(data){
$scope.phones = data;
}).
error(function(data, status, headers, config) {
alert('data = ' + data + ' status = ' + status);
});
}

在同一个目录(index.html的,controllers.js,phones.json)的所有文件。我缺少的是在这里吗?为什么它是抛出404错误?

All the files in same directory(index.html, controllers.js, phones.json). What am I missing here? Why it is throwing 404 Error?

下面是浏览器的附加截图:

Below is the attached screenshot of browser:1

推荐答案

如果该文件在同一目录不要紧。你需要一个服务器来托管文件,并在GET,POST PUT以及其他HTTP请求响应。

It does not matter if the files are in the same directory. You need a server to host the files and that respond to the GET, POST PUT and other http requests.

本教程的第一步要求您设置服务器。见标题为在与$ C $工作c。请参阅该步骤的意见/讨论过让有关设置服务器的详细信息。

The very first step of the tutorial requests you to set up a server. See the section titled "Working with code" in the tutorial. See the comments / discussions for that step too to get more information on setting up the server.

如果看起来的NodeJS是太难使用,那么一个简单的Python服务器也将这样做。你也可以使用Apache / WAMP / XAMP服务器上。

If nodeJs seems to be too difficult to use, then a simple Python server will also do. You could also use Apache / WAMP / XAMP server as well.

而无需架设服务器,你不能建立以来的http请求本教程中介绍将无法使用该应用程序 - 404错误是这样明显

Without having a server set up, you cannot work on building the application described in the tutorial since the http requests will fail - the 404 error is thus obvious.

这篇关于404错误在AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:56