AngularJS Upload Multiple Files with FormData API

How to POST FormData Using the $http Service When using the FormData API to POST files and data, it is important to set the Content-Type header to undefined. var fd = new FormData() for (var i in $scope.files) { fd.append(“fileToUpload”, $scope.files[i]); } var config = {headers: {‘Content-Type’: undefined}}; var httpPromise = $http.post(url, fd, config); By … Read more

Using success/error/finally/catch with Promises in AngularJS

Promises are an abstraction over statements that allow us to express ourselves synchronously with asynchronous code. They represent a execution of a one time task. They also provide exception handling, just like normal code, you can return from a promise or you can throw. What you’d want in synchronous code is: try{ try{ var res … Read more

The ‘Access-Control-Allow-Origin’ header contains multiple values

We ran into this problem because we had set up CORS according to best practice (e.g. http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api) AND ALSO had a custom header <add name=”Access-Control-Allow-Origin” value=”*”/> in web.config. Remove the web.config entry, and all is well. Contrary to @mww’s answer, we still have EnableCors() in the WebApiConfig.cs AND an EnableCorsAttribute on the controller. When we … Read more

Error with $http.get in angularJS — Success not a Function [duplicate]

The .success and .error methods are deprecated and have been removed from AngularJS 1.6. Use the standard .then method instead. $http.get(‘https://api.github.com/users’) .then(function (response) { var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; $scope.user = data; console.log(data); }); Deprecation Notice The $http legacy promise … Read more

Binary files corrupted – How to Download Binary Files with AngularJS

How to Download Binary Files with AngularJS When downloading binary files, it is important to set the responseType: app.service(‘VerDocServices’,[‘$http’,function($http) { this.downloadFile = function(url, file, urlDir) { var config = { //SET responseType responseType: ‘blob’, params : { file : file, urlDir : urlDir } }; return $http.get(url, config) .then(function(response) { return response.data; }).catch(function(response) { console.log(“ERROR: … Read more

Why are AngularJS $http success/error methods deprecated? Removed from v1.6?

The problem was that .success and .error methods are not chainable because they ignore return values. This caused problems for people familiar with chaining and encouraged poor code from people unfamiliar with chaining. Witness all the examples on StackOverflow that use the deferred anti-pattern. To quote one of the AngularJS team: IMO .success and .error … Read more

How do I POST urlencoded form data with $http without jQuery?

I think you need to do is to transform your data from object not to JSON string, but to url params. From Ben Nadel’s blog. By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, “application/json”. When we want to post … Read more