【发布时间】:2015-09-08 06:33:15
【问题描述】:
我是angularjs 的新手,我正在尝试使用$http.get 方法从json 文件中读取数据,但无法读取。
当我尝试使用$http.get 读取一个简单的txt 文件时,它读取正常。
我不明白我哪里错了......
这是我的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Plunkr.aspx.cs" Inherits="AngularJS.Plunkr" %>
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<title>content</title>
<script src="jquery-1.8.2.js"></script>
<script src="angular.min.js"></script>
<script type="text/javascript">
angular.module('myApp', [])
.controller('myController', function ($scope, $http) {
$scope.json = 'Cities not yet loaded.';
$http.get('read.txt') //when I try to read cities.json error occurs
.then(function (data) {
debugger;
$scope.json = data.data;
}, function (error) {
debugger;
alert('error');
});
})
;
</script>
</head>
<body data-ng-controller="myController">
<p>JSON content should display below here:</p>
<pre>{{json}}</pre>
</body>
</html>
read.txt 文件只包含Hello world 字符串。
当我尝试读取json 文件时then() 执行错误功能。
当我将鼠标悬停在 error 变量上时,它给了我 status 作为 404 和 statusText 作为 Not Found
这里是json文件city.json
{
"cities": [
{
"city": "Pune",
"latitud": "1",
"longitud": "2",
"id": "pun"
},
{
"city": "Mumbai",
"latitud": "45",
"longitud": "23",
"id": "mum"
},
{
"city": "Delhi",
"latitud": "22",
"longitud": "676",
"id": "del"
},
{
"city": "Chennai",
"latitud": "45",
"longitud": "787",
"id": "del"
}
]
}
我使用Visual Studio 作为IDE
我找到了这个例子here,它在那里完美运行。
【问题讨论】:
-
我无法理解 JSON 文件的错误是什么
-
是的,听起来你的端点有问题,而不是你的 json 文件。
标签: javascript json angularjs visual-studio