javascript 从 AngularJS 中的文本文件中获取 Json 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28387312/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:53:40  来源:igfitidea点击:

Getting Json data from text file in AngularJS

javascriptjsonangularjstextxmlhttprequest

提问by AnR

I am new to AngularJS and trying to fetch JSON data from a text file:

我是 AngularJS 的新手,并试图从文本文件中获取 JSON 数据:

Here is my HTML:

这是我的 HTML:

<div ng-controller="customersController as custCont"> 
  <ul>
    <li ng-repeat="x in names">
      {{ x.Name + ', ' + x.Country }}
    </li>
  </ul>
</div>

Whereas my controller is as given below:

而我的控制器如下所示:

app.controller( "customersController", function( $scope, $window) {
    $http({
        url: 'test.txt',
        dataType: 'json',
        method: 'POST',
        data: '',
        headers: {
            "Content-Type": "application/json"
        }

    }).success(function(response){
        $scope.names = response;
    }).error(function(error){
        $scope.names = 'error';
    });        

This doesn't show anything. Now if I replace the above http request with test.txt data assigned to $scope.names then it starts working: I mean, something like this:

这没有显示任何内容。现在,如果我用分配给 $scope.names 的 test.txt 数据替换上面的 http 请求,它就会开始工作:我的意思是,像这样:

$scope.names = [
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbk?p",
"City" : "Lule?",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"City" : "Graz",
"Country" : "Austria"
},
{
"Name" : "FISSA Fabrica Inter. Salchichas S.A.",
"City" : "Madrid",
"Country" : "Spain"
},
{
"Name" : "Galería del gastrónomo",
"City" : "Barcelona",
"Country" : "Spain"
},
{
"Name" : "Island Trading",
"City" : "Cowes",
"Country" : "UK"
},
{
"Name" : "K?niglich Essen",
"City" : "Brandenburg",
"Country" : "Germany"
},
{
"Name" : "Laughing Bacchus Wine Cellars",
"City" : "Vancouver",
"Country" : "Canada"
},
{
"Name" : "Magazzini Alimentari Riuniti",
"City" : "Bergamo",
"Country" : "Italy"
},
{
"Name" : "North/South",
"City" : "London",
"Country" : "UK"
},
{
"Name" : "Paris spécialités",
"City" : "Paris",
"Country" : "France"
},
{
"Name" : "Rattlesnake Canyon Grocery",
"City" : "Albuquerque",
"Country" : "USA"
},
{
"Name" : "Simons bistro",
"City" : "K?benhavn",
"Country" : "Denmark"
},
{
"Name" : "The Big Cheese",
"City" : "Portland",
"Country" : "USA"
},
{
"Name" : "Vaffeljernet",
"City" : "?rhus",
"Country" : "Denmark"
},
{
"Name" : "Wolski Zajazd",
"City" : "Warszawa",
"Country" : "Poland"
}
];

The text file obviously contains all the data except the first row (i.e. $scope.names = [and the last semicolon;

文本文件显然包含除第一行(即$scope.names = [最后一个分号)之外的所有数据 ;

That means the $http request to test.txt is failing which is in the same folder as HTML and JS files.

这意味着对 test.txt 的 $http 请求失败,该请求与 HTML 和 JS 文件位于同一文件夹中。

回答by nanndoj

You are missing to define $http as a parameter

您缺少将 $http 定义为参数

app.controller( "customersController", function( $scope, $window, $http) {

Also make sure you are testing in a web server. You cann't make ajax request from file:// protocol

还要确保您在 Web 服务器中进行测试。您不能从 file:// 协议发出 ajax 请求

Also change your request from POST to GET and it should work fine. Here is a Punklr

还将您的请求从 POST 更改为 GET,它应该可以正常工作。这是一个朋克

 method: 'GET',

回答by halfer

(Posted answer on behalf of the question author).

(代表问题作者发布答案)

There were two issues.

有两个问题。

  1. I missed $http parameter in my controller function.
  2. I was using "POST", which I replaced with "GET" to make it work
  1. 我在控制器函数中遗漏了 $http 参数。
  2. 我正在使用“POST”,我用“GET”替换它以使其工作

It now work from local machine as well as remote web server.

它现在可以从本地机器以及远程 Web 服务器工作。