Angular工厂返回index.html作为响应.data而不是JSON

Angular Factory returning index.html as response.data instead of JSON

本文关键字:data JSON 响应 返回 工厂 index html Angular      更新时间:2023-09-26

我在这个问题上迷路了。

我已经设置了一个productFactory,这意味着$http.get硬编码JSON数据。此JSON已被JSONlint验证。

返回的数据不是这个数据,而是我的index.html文件。是什么原因造成的呢?

productFactory:

angular.module('APP').factory('productFactory', function($http) {
    var productFactory = {
        getProducts:function(){
            return $http.get('/public/data/product.json')
                .then(function(response) {
                    console.log(response);
                    return response;
                });
        },
    }
    return productFactory;
})

记录到控制台的响应是我的整个index.html文件的字符串版本

/public/实际上是文件路径的一部分吗?通常公共文件夹被设置为文档根目录,实际上不会出现在任何资源的路径中。

尝试在浏览器中查看product.json,并确保Angular在同一路径上调用GET

你试过吗?

angular.fromJson(response);