-1

Here is an example of a .json file:

[
  {
     "name":"Jon Skeets"
  },

  {
     "name":"Bill Joy"
  }
]

If this json file is obtained as:

$http.jsonp(pathToFile).then() {
     ... 
}

It returns a 404. There is nothing wrong with pathToFile I have verified it with curl, wget and the browser. But the problem is with jsonp() having to fetch .json files with the above structure. $http.get() can parse .json files with the above structure. But JSONP cant. It needs a dictionary at the top level and not an array.

To demonstrate this, I have created this firebase: https://blazing-fire-6512.firebaseapp.com/name.json

fetch the link in your browser or wget and it works. Try fetching it with Angular here and it returns 404 Request failed.

InfinitePrime
  • 1,718
  • 15
  • 18
  • 2
    are you running code using any sever like xampp/django/tomcat? – Arpit Srivastava Aug 30 '14 at 13:53
  • @ArpitSrivastava: OP's last problem was on Firebase. http://stackoverflow.com/questions/25564200/obtaining-json-files-from-firebase. But I've been able to get such a structure from Firebase using `$http.jsonp` without problems as shown here: http://jsbin.com/musej/1/watch?js,console. – Frank van Puffelen Aug 30 '14 at 14:39
  • @ArpitSrivastava Yep I am using Apache. – InfinitePrime Aug 30 '14 at 14:55
  • @FrankvanPuffelen Please look at the question now. I have done as you said – InfinitePrime Aug 30 '14 at 14:56
  • How did you put that data into the Firebase? Because your JSON is returned quite differently from mine: https://stackoverflow.firebaseio.com/25564200/array.json – Frank van Puffelen Aug 30 '14 at 14:57
  • Hold on... that JSON is **not** in a Firebase. Is it a static file? – Frank van Puffelen Aug 30 '14 at 14:57
  • I am using firebase hosting. It is just a static JSON file. – InfinitePrime Aug 30 '14 at 14:57
  • Yeah it is a static file. Thats what I am saying. – InfinitePrime Aug 30 '14 at 14:58
  • @FrankvanPuffelen Am I doing something wrong? Should I put this in the firebase database? I did try. That dashboard knackered me. – InfinitePrime Aug 30 '14 at 15:05
  • Whether you should put certain data in Firebase depends on that data. Firebase is a hierarchical database that automatically synchronizes data (and changes to that data) to connected clients. Firebase hosting is a place to keep static files. If your JSON doesn't change, it is fine to put it in Firebase hosting. Or in any other static hosting place for that matter. – Frank van Puffelen Aug 30 '14 at 15:08
  • I am tempted to close this question as a duplicate of http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about. Anyone have an opinion on whether it's sufficiently similar or not? – Frank van Puffelen Aug 30 '14 at 22:09

1 Answers1

1

Firebase hosting does not support JSONP. It doesn't handle the callback parameter that is used to evaluate JSONP callbacks across origins.

Say you store this JSON at URL http://example.com/name.json:

{ "name":"Jon Skeets" }

If you access the URL http://example.com/name.json you get back exactly that JSON. And that is precisely what $http.get does.

When a server supports JSONP, it accepts a callback parameter. In such a case the URL ends up http://example.com/name.json?callback=displayName and the response is:

displayName('{ "name":"Jon Skeets" }');

This allows JSON data to be retrieved across origins, which is the whole reason for JSONP's existence.

Firebase's dynamic data servers support JSON. Firebase hosting does not.

Some possible solutions:

  1. If you're storing the JSON file and the application on Firebase hosting, you can just use $http.get.
  2. Since you control the JSON file, you can also simply put a function name inside name.json
  3. Store the JSON file at a host that supports JSONP. I often put them in Github Gists.
  4. Store the data structure in a Firebase hierarchical database, and not on their hosting servers
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • any other hosting providers you know that provide JSONP? – InfinitePrime Aug 30 '14 at 15:11
  • I think you typed that last question in the wrong box. Google is one tab to the right. But I provided at least one options in my latest edit just now. – Frank van Puffelen Aug 30 '14 at 15:18
  • Thanks for taking out the time to reply. I wont bug you now. If possible can you please explain the second option. Put a function name inside name.json? – InfinitePrime Aug 30 '14 at 15:26
  • What does the mean to function name inside JSON? myFuncName( [ { } ] ) Is it like this? – InfinitePrime Aug 30 '14 at 15:42
  • Yes. It (option #2) simply turns the .json file into a .js file. But why would you not simply go for option 1? If you're on the same server (that doesn't support JSONP), there is nothing against a simple, regular `get`. – Frank van Puffelen Aug 30 '14 at 19:12