Get header of existing page in angularjs - angularjs

Is there a way to get the header of the current page in angularjs, without having to do a separate call? I can't find this in the docs anywhere, but it seems like it should be possible.
E.g. if the file index.html contains the angular code, how do I read the header of the initial request to get index.html? I could use $http to make the call again, but that seems unnecessary.

If you are not handling the http request you won't have access to the request or the response, unless the code that it's making the request exposes that information somehow, which is not the case. So, I'm sorry but the answer is no.
However, if I may: what is exactly what you need from the response header? Because there could be other ways to obtain the information of the headers (i.e. cookies)

Related

angularjs $http with cache and interceptor

I'm trying to use angulars $http, with both a cache and an interceptor.
The quick question:
Currently when angular gets the answer from the server, it first caches it, and then it passes it through the interceptor.
Is it possible to force angular to first pass it through the interceptor and only then cache it?
The long issue:
The server responds every call with a format similar to:
{permission: BOOL, data:[...]}
In the interceptor response I check the permission, and if correct I throw it away and pass only the data field to the application level. If it fails I reject and popup an error, etc... (Maybe I should do it in a transformResponse function, but I'll endup facing the same issue).
Then for some API calls I'm requesting a bunch of resources like that:
/resource/ALL
And it obviously caches this request and answer, but what I want to do next is fake caching every resource that I received.
So forthcoming calls to /resource/{some resource id} are already cached, cause I've already received it in the call where I requested ALL.
The problem I'm facing is, when I want to fake cache, on the application level, I lost the "{permission: BOOL" part, cause I've thrown it in the interceptor.
Some notes:
1- Of course I could also fake the permission part, and just hardcode it, but I feel it's not an option since if I later add / modify / remove metadata it's another place I've to look at.
2- An other way would be to don't throw the metadata on the interceptor / transformResponse, but again this is not an option since I don't want to select the 'data' field every time I call $http on the application level.
So I think the best option for me would be to cache after the interceptor, and not before.
Hope I made the issue clear, and any answer is welcome!

What is the best way to route to different controllers in CakePHP depending on HTTP header values?

I'm looking for a way to route URLs to different controllers depending on the value of the HTTP 'Accept' header.
All I have found so far is the official CakePHP documentation on routes but it is not very detailled.
Is this possible using route configurations or do I have to do this another way (e.g. by implementing a DispatcherFilter to inspect the CakeRequest)?
I want a set-up where I can seamlessly distribute responsibilities
between server side and client side: e.g. letting the client handle
the view creation instead of the server responding with an HTML
document.
Guess you're using something like AngularJS? Well, then you expect a data format back instead of HTML I guess. So request application/json via the accept header and / or the extension. Cake can detect both and respond accordingly.
Read about JSON and XML views in the manual.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html

AngularJS getting data from backend

I would like to know what is the proper way to get data from backend when I want to use angularJs (or similar) in my web app?
The only way I see is to render html (static html with js scripts - e.g. angularjs) with no data from backend and then download data via ajax requests from my backend API. But I think this solution is not good because of many HTTP requests:
For example I have blog website, I want to show a post, comments, and the related posts on the sidebar. So probably I need to make at least 3 HTTP requests to get the data unless I will prepare API to get all I need in one request.
I can also imagine websites that could have much more HTTP requests. Is it a proper way to do this? Doesn't it overload a server? Or my way of thinking is so wrong?
It is either websockets or HTTP requests. Preparing API to get all in one request is one option. Another two options are XMLHttpRequest/iframe streaming which is a method of a technique known as Comet.
I would go with websockets since it is supposed to solve the problem that was previously solved with weird applications like iframe streaming. There are libraries that properly handles fallbacks if the browser does not support websockets:
web-socket-js ( this needs a websocket server )
Socket.IO ( this has a node.js module and also implements a kind of unnecessary protocol on top of websocket protocol )
If you choose the old methods there will be many problems waiting for you on the road like XmlHttpRequest.responseText while loading (readyState==3) in Chrome
I think you have to distinguish two cases:
You render the page for the first time.
You update parts of your page when something changes
Of course in the second case it makes sense to fetch only parts of the page via individual HTTP requests. However, in the first case you can simply serialize your complete model as one JSON object and embed it in the page like this:
<script type="text/javascript">
var myCompleteModel = { /* Here goes your model */ };
<script>
The controllers of the components on your page can then access this global variable to extract the parts being relevant for them. You can also wrap access to the initial model in a service to avoid accessing a global variable in all your controllers.

AngularJS Directive to check if an image URL exist

i'm trying to write an AngularJS Directive that checks if a URL exist, if it does then it will display the image. Otherwise it will return nothing or a blank tag. Any advice would be appreciated, here's the fiddle I have so far, can't seem to get it to even read the url being passed in.
http://jsfiddle.net/stevenng/dXeap/8/
You will only be able to this with images on the same domain as it would violate cross-domain policy otherwise. If that is still ok then it should be possible by using AJAX to do a HEAD request and checking the HTTP status (e.g. 404) of the response.
I fixed up your code a bit so that it works. http://jsfiddle.net/LwpEP/1/
See how you get on from that starting point and come back here if you run into more issues.

how to send/receive data securely in backbone.js

I have created RESTFUL urls that respond with some JSON data when fetched by backbone.
So a url like /lists responds with a json array of user-created lists. My want that if the url is accessed by address bar input like mydomain.com/lists, the json data is displayed in text on browser. I want the server to respond only if the url is accessed from within the application. Can somebody provide me some hints on how to achieve this?
Like #Thilo said, you're not going to be able to do prevent a person with the right tools to see what's coming across the wire, Firebug's console/net tabs already keep track of requests and show the contents of responses.
That being said, what you can do is check whether the HTTP_X_REQUESTED_WITH HTTP header is set to 'XMLHttpRequest', and only return the JSON in this case. Backbone makes an Ajax call so this will always be the case with the Backbone calls (in modern browsers). Again this won't help much except for people who type it into the address bar directly (and do a normal GET request) won't see the JSON.

Resources