0

I have written a simple app in angular, where I have encoded the json data directly into the $scope of a controller, and then I do stuff with that data. Can I have something like data.json, and send it to the client along with js and css files, which angular can then read? How would I go about doing that? Ajax is not allowed.

yayu
  • 7,758
  • 17
  • 54
  • 86
  • Your question is unclear: a file could not be a server... Or even you have to reach `file:///` URI form... Maybe using some tool like *netcat*: `nc -l -p 8080 < <(echo $'200 OK\r\nContent-type: application/javascript\r\n\r';cat file.js)`? – F. Hauri - Give Up GitHub Sep 10 '14 at 22:16
  • @F.Hauri I am working on github pages, which forbids ajax – yayu Sep 10 '14 at 22:18
  • You have to use a server to be *listenning* for incomming requests. Github is not a *website holder*. – F. Hauri - Give Up GitHub Sep 10 '14 at 22:21
  • @F.Hauri Yes. But they allow you to serve static assets such as html, css, image files, etc. I want my json file to be one of those static assets sent to the client that the javascript then easily read (instead of hardcoding it as I've been doing right now) is that possible? – yayu Sep 10 '14 at 22:27
  • 2
    I don't see why you would not be able to make a AJAX request to get a JSON file, serving a file is serving a file. A static server means that the server cannot run any server side code (PHP, ASP, etc). It doesn't mean that a client cannot dynamically request static files (such as .json, .js, .css, etc) from the server. – Useless Code Sep 10 '14 at 22:36
  • Maybe is there an issue about extension choosed! Try to use one of `.js`, `.json` and `.txt`. – F. Hauri - Give Up GitHub Sep 10 '14 at 22:41

1 Answers1

0

If AJAX is not allowed you will have to make the file a javascript file, not a JSON file. By doing this you get something very similar to JSONP (JSON with Padding). In this, you essentially pass the JSON object to a callback function. For example:

myCallback({"foo":"bar", "bing":1900});

You can then include this file as a script tag and your callback will be fired with the appropriate data. Ensure that you include the script tag for the data after the script tag that defines the callback function.

The other alternative is storing the object as a global variable. It's generally considered bad to pollute the global scope.

Jessie A. Morris
  • 2,267
  • 21
  • 23
  • JSONP is usually considered to be Ajax. – Quentin Sep 10 '14 at 22:07
  • Maybe, but it should fit within the parameters that he's defined. It's simply another script tag that calls a function with his data. It doesn't require anything other than having another script tag. – Jessie A. Morris Sep 10 '14 at 22:11
  • The parameters that he's defined include "not Ajax". It doesn't fit in with that. – Quentin Sep 10 '14 at 22:12
  • It's a script tag with a callback function and data. How is that AJAX? If that's the case, his whole application is AJAX since he has to have script tags and he should just go home now. – Jessie A. Morris Sep 10 '14 at 22:13