AngularJS JSONP GET Example
This is an example of a cross domain GET request using JSONP, the most common way to get data. Like the previous example using CORS, this too uses requires the server to be ‘aware’ of request so it can reply with the JSONP callback method.
AngularJS CORS example using httpbin.org
This is an example of a cross domain GET and POST request to a server with CORS headers enabled. It’s a very simple one field form that displays the echo’d response from httpbin. I chose httpbin since I knew it had CORS headers enabled, and seemed like a relatively simplistic tool to play around with.
As the code shows, GET requests are quite simple and look like a regular $http call. The heavy lifting is all done by the server
The POST request on the other hand is a little more complicated and requires some manipulation of the headers before the request itself.
First, we need to set the Content-Type to form-urlencoded to let the destination know we are posting form data:
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
Second, we need to delete a header called “Requested-With” that is sent along with AJAX requests:
Also, to stop this mistruth from spreading any farther: A lot of blog posts about Cross Domain posting with Angular will show you this code:
$http.defaults.useXDomain = true
It’s not a real config value, you don’t need it. Seriously. Try it with and later without it, you won’t see a difference (I think the dojo toolkit may require it, I’m not sure how it made it’s way into multiple AngularJS examples).
A talk John and I gave on D3 & MeteorJS at our last MeteorJS LA Meetup. We touched on some D3 basics, and discussed some challenges we faced integrating it with MeteorJS.
code available at: https://github.com/ArroyoLabs/meetups