无法使用get post angular从服务器加载json文件 [英] Not able to load json file from server using get post angular

查看:75
本文介绍了无法使用get post angular从服务器加载json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey I'm trying to use $http get function from angular to retrieve a json file from server located at **"http://localhost/week10/resources/dog.json"** 
But I can't seem to access the file. The code works fine when I use the file in the local destination. 

My code looks like this


This is the angular file responsible for getting data

    app.controller('myCtrl', function($scope, $http) {
        $http({
            method : "GET",
            url : "http://localhost/week10/resources/dog.json"
        }).then(function mySuccess(response) {
            $scope.dogObj = response.data;
        }, function myError(response) {
            $scope.dogObj = response.statusText;
        });
    }); 


The code for displaying these characters 


    <ul class="row">
    	<li class="col-lg-4" ng-repeat="x in dogObj">
    		    
    		    <div class="thumbnail">
    		     <a href="view/view2/{{x.actual_name}}"> <img src="{{x.src}}" ></a>
    		    </div>
    
    	 </li>
    </ul>


And the json looks like this

    [{"actual_name":"dog1.htm","shown_name":"dog1","src":"images/dog1.jpg"},
                              {"actual_name":"dog2.htm","shown_name":"dog2","src":"images/dog2.jpg"},
                               {"actual_name":"dog3.htm","shown_name":"dog3","src":"images/dog3.jpg"},
                               {"actual_name":"dog7.htm","shown_name":"dog7","src":"images/dog7.jpg"},
                               {"actual_name":"dog4.htm","shown_name":"dog4","src":"images/dog4.jpg"},
                               {"actual_name":"dog5.htm","shown_name":"dog5","src":"images/dog5.jpg"},
                               {"actual_name":"dog6.htm","shown_name":"dog6","src":"images/dog6.jpg"},
                               {"actual_name":"dog8.htm","shown_name":"dog8","src":"images/dog8.jpg"}]


The code works file when I change the 
**url:dog.json** as there is a dog file in a local destination as well

but when I change it to **http://localhost/week10/resources/dog.json** The site doesn't work. 

If I simply put the file into the browser the json file shows up. 

The browser inspect console shows xml parsing error.





我尝试过:



我有交叉来源错误我将头文件添加到apache配置文件中,

这清除了firefox上的交叉源错误但是仍然出现在chrome



What I have tried:

I had cross origin error I added header file into the apache config file,
This cleared the cross origin error on firefox but still comes up on chrome

推荐答案

http从angular获取函数以从位于**的服务器检索json文件http:// localhost / week10 / resources / dog .json**
但我似乎无法访问该文件。当我在本地目的地使用该文件时,代码工作正常。

我的代码看起来像这样


这是负责获取数据的角度文件

app.controller('myCtrl', function(
http get function from angular to retrieve a json file from server located at **"http://localhost/week10/resources/dog.json"** But I can't seem to access the file. The code works fine when I use the file in the local destination. My code looks like this This is the angular file responsible for getting data app.controller('myCtrl', function(


scope,


http){


这篇关于无法使用get post angular从服务器加载json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆