JSONP Callback function $http
Hi i'am quite new in angular.
I'am triying to use the $http module for xhr request.
However, the callback name must be "JSONP_CALLBACK" in which angular will turn the callback name to callback=angular.callbacks._1
There's some web web service which cannot accept "angular.callbacks._1 "callback name, for example
http://dev.markitondemand.com/Api/Lookup/jsonp?input=GM&callback=callback=angular.callbacks._1
i solve this with a messy hack found on stackoverflow
http://jsfiddle.net/pMGgR/
me too - using the windows ACS service and it doesnt like the call back name
@raghur @todiadiyatmo do you guys know what upsets your JSONP endpoints with the callback names generated by AngularJS? Is it a dot? Or underscore? Or sth else altogether?
Getting rid of a dot might be problematic since callbacks would have to be registered in a global (window) scope....
I had the same problem with the TrustYou API. I suspect the dot being the issue ... I also came across the hack on stackoverflow but I decided to use jQuery.getJSON in this particular case instead
@pkozlowski-opensource - In the Chrome network console, if I click on the url to ACS, I get an ACS error page that tells me that the callback name is invalid.
@haraldringvold I would sugest opening a new issue if you are having one, otherwise stackexchange would be the best place to ask a more general question like this.
I'm having the same trouble with a third party API (all the dots being removed from the callback function name). Since my search brought me here I figure adding the following snippet here might be useful for others who find themselves in the same situation.
Add the following immediately before making a jsonp request.
var c = $window.angular.callbacks.counter.toString(36);
$window['angularcallbacks_' + c] = function (data) {
$window.angular.callbacks['_' + c](data);
delete $window['angularcallbacks_' + c];
};
The main problem is that this relies heavily on angular's current jsonp implementation and is likely to need maintenance in the future.
As a more permanent solution the framework could have a low level service purely for registering globals (heavily and uniquely prefixed so that multiple angular apps don't collide), which would also make cleaning up after an app much simpler.
As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.
Please try the newest versions of Angular (1.0.8 and 1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.
Thanks!
I am getting the same issue on the CIneworld API, but I have used it without issue on other API's. It definitely seems as though some web services can't accept a callback name in the format "angular.callbacks._1"
It happens whether I use $http.jsonp, $http.get or .get as part of a service with AngularJS 1.0.7 and also 1.0.8.
I can use jQuery instead or the alternative fix (http://stackoverflow.com/questions/13267285/angular-resource-with-jsonp-not-working), but it would be nice to use the full features of AngularJS.
I tested it with 1.0.8 & 1.2.0rc1 and have the same problem with both.
This isn't working for me.
version: 1.0.8 OS: Mountain Lion
I did a JSONP request on one of the file hosted at Github, like so
$http.json("https://raw.github.com/<username>/<reponame>/master/somedata.json?callback=JSON_CALLBACK")
ATM, the work-around mentioned by @williamtdavies worked for me
cheers
Let's move this discussion to #3073
There's no point in merging this to #3073 because the problem caused by this bit:
'angular.callbacks.' + callbackId
which still exists in that pull request. That is the dots will still be in callback=... and hence not all endpoints out there in the wild will work. I'm using 1.2.4.
(I'd like to point out that I've watch/read a few tutorials and I was impressed with angular, until I wrote
$resource('http://...', { ..., callback: 'JSON_CALLBACK' }, ...);
as my 10th ever line of angular code, now I am really disappointed!)
This is also a problem for me.
"ReferenceError: JSONP_CALLBACK is not defined" on fully valid JSONP
Hi!
I also have the problem. An API is ignoring the provided callback if it contains a dot. I have tried @dancras workaround but it doesn't work for me because I am doing parallel requests and the first few ones will use the same callbacks and the results will be mixed for the next ones.
Honestly, if an API can't deal with a dot or square brackets for the callback name, then that's really a bug to file on the API. You should typically only need to use JSONP in small demo applications, since larger scale apps should have the resources to set up CORS properly and avoid issues with XMLHttpRequest.
Strongly advise you bring this up with the web service you're having trouble with.
Ultimately yes, but this thread seems to show that many API have those restrictions. I am dealing with a commercial API that I am unlikely to be able to change in a short timeframe. It supports CORS but it also requires basic authentication and they didn't configure the CORS part for that (they don't allow the Authorization header and they return a wildcard as allowed origin).
Polluting the global namespace is not really in the cards, so this is a problem for web services to deal with --- this is not an angular issue.
While I can see @caitp's perspective:
--- this is not an angular issue.
Do you not think its a good approach to try cater for environments?
For example, what would jQuery be, if they just said "this is not a jquery issue" every time they ran into a browser issue?
It's not a browser issue either, it's literally "your web service needs to be a bit better and let you specify callbacks as properties of objects". Without this, all you can do is make it call global functions on window, and I don't think it's really in the cards for angular to support this
In an ideal world, that's true (also it's not "our" web service, it's third party), but there are more rigid platforms/services/companies/processes. There's no business value in making any HTTP endpoint modification to support calls from Angular, however if Angular's view is more flexible (i.e. you relax your statement and allow some exceptions when necessary) it'll be more widely used. I think there are more web services out there in the wild not supporting this than the number of seconds it may take to you fix (implement) this.