简体   繁体   中英

Angular $http.jsonp - Server don't accept JSON_CALLBACK

All is in the question.

I have to use an API that only lets me make requests with angular using $http.jsonp. I get the response correctly from the server, but angular considers that the request fails as I'm not using callback=JSON_CALLBACK . And when Im adding the callback in the request, the server send me back an error 500 as it doesn't allow any other parameters than the ones in the docs.

What can I do? Im at a loss

Thanks

In order for JSONP to work it requires server-side support. If the API you are using does not provide any support for JSONP than you simply cannot use JSONP.

In order for JSONP to work the server must wrap the JSON it returns in a function call. Usually, an API that supports JSONP will have some sort of way of passing the name of a function it can use as a callback. This is not standard, and each API might have a different way to specify the callback. If the API doesn't support specifying a callback, than you are really out of luck.

The only other thing I can think of is if the server the API is on supports CORS. CORS is a better alternative than JSONP for making cross-domain requests, but only works on modern browsers.

The only other alternative is to have your own server proxy the request on behalf of the browser. This has the downside that all traffic to that third-party API must travel through your own application's domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM