Setting the JSONP callback function in AngularJS - angularjs

I'm trying to fetch data from a web API via Angular $resource service. The service exposes JSONP interface, but does not allow setting the callback name. Everything works well, my requests goes out, the data returns, the script is injected and then it fails because the callback function is not defined.
Angular documentation is very sparse on this, but it seems that the default callback function Angular sets up is: JSON_CALLBACK, and there's no info how to change that so that it matches the function returned by the foreign API.
Thanks.

I don't think that there is any provision to override that callback.
$resource is high level Restful api based on $http service.
You can use $http apis which returns http promise object and letting you write your success callback wherein you can process data returned from ajax request.
e.g. http://docs.angularjs.org/api/ng.$http#jsonp

Related

In NestJS how to get request object in service without injecting request from controller

How can I inject the request or the execution context in a service without the help of controller without any performance issues?
The docs make it very clear that to pass the request to a service you can use a REQUEST scoped service with #Inject(REQUEST) or you can get the request in the controller via #Req() and pass that to the service method.
There is nestjs-cls which may be helpful as well, but generally, you either pass it on or you take the performance hit for REQUEST scoping the provider.

How to use two separate $http service instances in AngularJS?

I am implementing an angularjs service, which saves the data sent by an $http call in localStorage. In order to do that, I am using the request interceptor, so that whenever an http request is sent via $http, the data is saved in localStorage. Below is my code for the interceptor,
var OfflinkJs = angular.module('OfflinkJs', []);
OfflinkJs.factory('cacheInterceptor', function () {
var cacheInterceptor = {
request: function (config) {
// Here I am saving the config as a string in localstorage
return config;
}
};
return cacheInterceptor;
});
For above interceptor to work, I have to register it in the interceptors array of $httpProvider. I have done this to achieve that,
OfflinkJs.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('cacheInterceptor');
}]);
PROBLEM
Now, when I use OfflinkJS module in another module, all the $http calls go through my interceptor. But I would like to make some requests sent by $http service use my interceptor while some other requests NOT.
Since $http service is a singleton, I cannot figure out how to use two instances of it in separate places of my application. Is there any way to achieve this?
I went through this question, but seems it really addresses the issue of Circular dependency
I need two instances of AngularJS $http service or what?
I check the URL in the interceptor and use that to filter out requests to other services. I set the base url for my service as a constant in my module, and then check against that. If the request isn't to the relevant service, it just passes through with no action.
But perhaps a better way would be to set up a data service instead of an interceptor. There are plenty of tutorials out there on data services.

Restangular with angular-http-auth not working

I had been using Angular-HTTP-Auth with the basic http requests, but have since moved to Restangular and are now finding that 401's are no longer caught by HTTP-Auth. I'm assuming this is because the Angular http methods aren't used or are intercepted somehow. Is there a method for making these work together again? I know that Restangular has interceptor's exposed, but I'm not certain how the 2 need to operate.

providers with $http wrapper angularjs

I would like to know whether we can create a provider in angularjs which will replace the $http operation .which means where we can use this provider in other modules where we can make use of these $http operation.
The reason why provider has to be taken is because we can configure the http parameters like the api path, request type .
Also can we have logging/exception handling mechanism inside the provider so that the modules(eg: any other factories) which inherit the provider wont need to do any extra logging/exception mechanisms.
Is there any way to have some loading screen using this provider when http requests are made ?
For the things you mentioned, you don't need another provider, because $http has the concept of interceptors.
Interceptors can specify different callbacks to be executed at different phases:
request (runs before any request is sent): It can modify the configuration (e.g. the request URL, method etc). It could also be used to show some loading message/animation (e.g. using some property on the $rootScope).
requestError (runs when there is an error before sending the request): It can be used for logging, recovering, exception handling.
response (runs after any response is received): It can be used for logging. It could also be used to hide the loading message/animation. (Don't forget to also handle this on response error.)
responseError (runs when there is an error regarding the response (e.g. bad request)): It can be used for logging, recovering, exception handling.
If interceptors do not cover your needs, you could use $provide's decorator to monkey-patch, augment or totally replace the $http service:
.config(function ($provide) {
$provide.decorator('$http', function ($delegate) {
var newHttp = $delegate; // or a totally new object
// ...monkey-patch newHttp or define new methods or whatever
return newHttp;
});
});

I want my AngularJS response interceptor to have access to the un-transformed response

I have an $http response interceptor, and I want to have access in the interceptor function to the raw response object--not the return value of transformResponse. Is this possible?
A little background: every response gets wrapped in a JSON object containing various metadata such as API version, detailed status information, etc. My transformResponse method strips out this wrapper and returns only the actual response payload, because I don't want the various controllers and services to be using this metadata.
However, I would like to have interceptors that do things like complain if the API version is incorrect, or handle various abnormal status conditions, so they need access to that metadata.

Resources