【问题标题】:fetch json file into angularjs将json文件获取到angularjs
【发布时间】:2017-01-16 16:31:30
【问题描述】:

我正在尝试在我存储了几个数组的本地服务器上获取一个 json 文件。我试图通过使用代码运行如下的 $http 服务来实现这一点:

(function(){
    var app = angular.module('store', ['store-products']);
    app.controller('StoreController',['$http', function($http){
        var store = this;
        store.products = [];
        $http.get('products.json').success(function(data){
            store.products = data;
        })
})();

还有我的 products.json 文件:

[ {
    "name" : "Lasagna",
    "price" : 250,
    "images":[{"full":"images/lasagne.jpg"}],
    "Description":"Lasagne are wide, flat-shaped pasta, and possibly one of the oldest types of pasta.The word 'lasagne', and, in many non-Italian languages, the singular 'lasagna', can also refer to a dish made with several layers of lasagne sheets alternated with sauces and various other ingredients.",
    "canPurchase": true,
    "Reviews":[]

            },
            {
    "name":"Shawarma",
    "price":300,
    "images":[{"full":"images/shawarma.jpg"}],
    "Description":"Shawarma or Shawurma  is a Levantine meat preparation, where lamb, chicken, turkey, beef, veal, buffalo meat, or mixed meats are placed on a spit (commonly a vertical spit in restaurants), and may be grilled for as long as a day.Shavings are cut off the block of meat for serving, and the remainder of the block of meat is kept heated on the rotating spit. Shawarma can be served on a plate (generally with accompaniments), or as a sandwich or wrap. Shawarma is usually eaten with tabbouleh, fattoush, taboon bread, tomato, and cucumber. Toppings include tahini, hummus, pickled turnips, and amba",
    "canPurchase": true,
    "Reviews":[]
    }]

还有我的html代码:

<!doctype html>
<html ng-app="store">
<head>
     <link rel="stylesheet" href="styles/bootstrap.min.css">
</head>
<body ng-controller="StoreController as store">
    <h1 align="center"><ul>Food Junkie</ul></h1>
    <h2 align="center">-It's all gonna be alright-</h2> 
    <ul class="list-group">
        <li class="list-group-item" ng-repeat ="product in store.products">
            <product-title></product-title>
            <product-images></product-images>
            <button ng-show="product.canPurchase"><img src="images/cart_64x64.jpg"/></button></br>
            <product-panel></product-panel>
        </li>
    </ul>

    <script type="text/javascript" src="scripts/angular.min.js"></script>
    <script type="text/javascript" src="scripts/ang.js"></script>
</body>
</html>

【问题讨论】:

  • 当您直接从网络浏览器或REST客户端请求&lt;server_url&gt;/product.json时,您会得到什么结果?
  • @AsielLealCeldeiro:我得到了我列出的数组。

标签: javascript angularjs arrays json


【解决方案1】:

你可以这样访问它,但是你的网址是错误的。如果 json 文件在根目录中,只需使用$http.get('product.json'),否则不要以破折号开头/,即:$http.get('path/to/product.json')

请注意您的 Angular 版本,因为最新版本已将 $http .success().error() 替换为 .then().catch()

【讨论】:

  • 是的,我尝试删除反斜杠,但没有任何改变,我使用的 angularjs 的缩小版本存储在我的本地服务器(不是 CDN)中,我确定它不是更新版本.
  • 您遇到什么错误? 404?如果您正在提供 json,您是否尝试过使用绝对路径而不是相对路径?
  • 是的,当我提供 .json 文件的路径时,我得到了列出的数组,但我在 js 文件中提到的路径只是文件名,因为它位于我的根文件夹中。
  • 您应该使用绝对路径或定义根目录:
  • 我将路径更改为“localhost/project_2/products.json”,但没有任何改变
猜你喜欢
  • 2015-04-07
  • 2018-03-25
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多