Sunday, 15 May 2011

restangular - capturing full URL of a GET request in AngularJS including query params -


I am using HTTP interceptor when I make a reliable GET request, then I see all the values ​​here my request interceptor Is the code for

  Request: view the link below to see the value of {// config console.log ('config', config); // below the query string console log ('location', $ location.absUrl ()) returns the complete URL; // $ rootScope.cacheData $ cacheFactory console.log ('$ rootScope', $ rootScope.cacheData); // Return only (id: "http", size: 3} console.log ('cache information', $ rootScope.cacheData.info ()) // Returns uncertain console.log ('receive cache', $ rootScope .cacheData.get ('http')) // // is not related to // since the code has been removed ........ // ........ Return the query or spaces Wrap it in promise Return Revolutions || $ Q.when (config);},  

Config Value:

Unfortunately, manually-captured consultation ready Do not guarantee 100% guarantee that this is a cached match I noticed that cacheFactory checks the exact string that was requested. So if the query parameters of our GET request are age = 12 & amp; name = Scott on our John Interceptor, then let us name it first (name = Scott & amp; Age = 12), the cachefactor will not be able to find it.

So I'm trying to find an angular service or factory that will return the full URL equivalent to the request made by us. A. I tried $ space but it is not giving full GET request.

I just decided to parse the config, make it from scratch, it's working great :)

  if (config.method == 'GET' & amp; (config.url.indexOf ('v1 / repeters')! = -1)) {/ / full path var cachedUrlsLocalStorage ready ; Var absolute path = ''; _ (Config.params) .keys (). Sort (). Each (function (key, index) {var value = config.params [key]; absolutePath = absolutePath + key + '=' + value + (index & lt; _ (Config.params) .keys (). Value () Length - 1? 'Amp;': '');}); CachedUrlsLocalStorage = JSON.parse (window.localStorage.getItem ('cachedUrls')); If (cachedUrlsLocalStorage) {var exists = _.findIndex (cachedUrlsLocalStorage, function (cached data) {return cachedData.url == config.url + '?' + AbsolutePath;}); If ((exists!) {CachedUrlsLocalStorage.push ({url: config.url + '?' + AbsolutePath, timeout: moment (), end time: moment (). Add (10, 'min')}); Window.localStorage.setItem ('cachedUrls', JSON.stringify (cachedUrlsLocalStorage)); }} Else {cachedUrlsLocalStorage = []; CachedUrlsLocalStorage.push ({url: config.url + '?' + AbsolutePath, timeout: moment (), end time: moment (). Add (10, 'min')}); Window.localStorage.setItem ('cachedUrls', JSON.stringify (cachedUrlsLocalStorage)); }}  

No comments:

Post a Comment