Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:
JavaScript
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.
JavaScript
<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.
Posted
Updated 9-Oct-15 19:45pm
v2

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900