如何从服务器获取JSON文件 [英] How to get a JSON file from a server

查看:148
本文介绍了如何从服务器获取JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从具有AngularJS $ http请求的服务器获取JSON文件。

我在服务器上没有运行服务,这只是从特定文件获取文件的请求位置。



这是我正在使用的代码:

  var  urlJSON = '  http://www.somewebsite.com/test.json'; 
$ http({
method:' GET'
url:urlJSON
})
.then(
function success(response){
alert( 成功');
},
function 错误(消息){
alert(' 错误:' +消息。 status + ' \\\\ n' + message.config.url + ' \\\\ n' + message.statusText);
}
);



这在IE11中工作正常,但在FireFox,Chrome和Edge中,调用错误函数时status = 0且statusText为空,因此没有太多信息可以继续。



我使用的AngularJS版本是1.4.5。

< script type =   text / javascript src =   http://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js>< script> 





服务器是由GoDaddy和cPanel托管的Apache服务器。

JSON文件的类型为text / x-generic,如果这与我的问题相关。



当然,浏览器和我已经错过了一些配置或必须要做的事情来使它工作。

问题是我不熟悉Web编程,特别是AngularJS,我无法想象我的拥有。



任何帮助都将不胜感激。



 [更新] 
如果我试图从服务器获取一个不存在的文件,我在IE11中获得status = 404但是对于其他浏览器,状态是st生病了。

解决方案

http请求。

我没有在服务器上运行服务,这只是从特定位置获取文件的请求。



这是我正在使用的代码:

  var  urlJSON = '  http://www.somewebsite .com / test.json'; 


http({
method:' GET'
url:urlJSON
})
.then(
function success(response){
alert(' Success');
},
function 错误(消息){
alert('' 错误:' + message.status + ' \ r \' + message.config.url + ' \\\\ n' + message.statusText);
}
);



这在IE11中工作正常,但在FireFox,Chrome和Edge中,调用错误函数,状态= 0并且statusText为空,所以没有太多信息可以继续。



我使用的AngularJS的版本是1.4.5。

< script type =   text / javascript src =   http://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js>< script> 





服务器是由GoDaddy和cPanel托管的Apache服务器。

JSON文件的类型为text / x-generic,如果这与我的问题相关。



当然浏览器有所不同,我有错过了一些配置或必须要做的事情才能使它工作。

问题在于我是一般的Web编程和AngularJS的新手ar,我无法自己解决这个问题。



任何帮助都将不胜感激。



 [UPDATE] 
如果我尝试从服务器获取一个不存在的文件,我在IE11中获得status = 404但是对于其他浏览器,状态仍为0。 / pre>


我终于发现了问题所在。通过在Chrome中使用F12打开开发人员窗口我看到我有错误

请求的资源上没有'Access-Control-Allow-Origin'标头。因此不允许原点'null'访问。



所以这是CORS,跨源资源共享的问题。



我遇到此问题的原因是因为我在不使用本地服务器的情况下从我的C:驱动器加载HTML页面和Javascript文件,然后我尝试从远程服务器加载JSON文件。

(我忘了在我原来的帖子中提到这个。)



似乎所有新浏览器都阻止了此次通话,而IE11(可能也是旧版本)允许它。



当我将HTML和JS文件上传到服务器时,它可以正常工作。



注意

对于Chrome,您可以从命令提示符打开浏览器,其参数如下所示:

 chrome.exe --user-data-dir =C:/ Chrome dev session--disable-web-security 



将使您能够在开发阶段尝试此类调用。


I am trying to get a JSON file from a server with an AngularJS $http request.
I have no service running on the server, this is just a request to get a file from a specific location.

This is the code I am using:

var urlJSON = 'http://www.somewebsite.com/test.json';
$http({
    method: 'GET',
    url: urlJSON
})
.then(
    function success(response){
        alert('Success');
    },
    function error(message){
        alert('ERROR: ' + message.status + '\r\n' + message.config.url + '\r\n' + message.statusText);
    }
);


This works fine in IE11, but in FireFox, Chrome and Edge the error function is called with status = 0 and the statusText is empty, so not much information to go on.

The version of AngularJS I am using is 1.4.5.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"><script>



The server is an Apache server hosted by GoDaddy and cPanel.
The JSON file has the type "text/x-generic", if that is relevant for my problem.

Of course there is a difference in the browsers and I have missed some configuration or something that has to be done to make it work.
The problem is that I am new to web programming in general and AngularJS in particular, and I cannot figure this out on my own.

Any help would be appreciated.

[UPDATE]
If I try to get a file that doesn't exist from the server, I get status = 404 in IE11 but for the other browsers the status is still 0.

解决方案

http request.
I have no service running on the server, this is just a request to get a file from a specific location.

This is the code I am using:

var urlJSON = 'http://www.somewebsite.com/test.json';


http({ method: 'GET', url: urlJSON }) .then( function success(response){ alert('Success'); }, function error(message){ alert('ERROR: ' + message.status + '\r\n' + message.config.url + '\r\n' + message.statusText); } );


This works fine in IE11, but in FireFox, Chrome and Edge the error function is called with status = 0 and the statusText is empty, so not much information to go on.

The version of AngularJS I am using is 1.4.5.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"><script>



The server is an Apache server hosted by GoDaddy and cPanel.
The JSON file has the type "text/x-generic", if that is relevant for my problem.

Of course there is a difference in the browsers and I have missed some configuration or something that has to be done to make it work.
The problem is that I am new to web programming in general and AngularJS in particular, and I cannot figure this out on my own.

Any help would be appreciated.

[UPDATE]
If I try to get a file that doesn't exist from the server, I get status = 404 in IE11 but for the other browsers the status is still 0.


I finally found out what the problem was. By using F12 in Chrome to open the developer window I saw that I had the error

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.


So it was a matter of CORS, Cross-Origin Resource Sharing.

The reason I encountered this problem was because I was loading the HTML page and Javascript files from my C: drive without using a local server and then I tried to load the JSON file from a remote server.
(I forgot to mention this in my original post.)

Seems like all new browsers prevent this call, while IE11 (and presumably also older versions) allows it.

When I uploaded the HTML and JS files to the server it worked as it should.

Note
For Chrome you can open the browser from the command prompt with the parameters shown below:

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security


This will enable you to try this kind of calls during the development phase.


这篇关于如何从服务器获取JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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