0

The below code works if I use jquery ajax but $http of Angular doesn't send any data to the server using the code below:

myapp.factory('startCampFactory',function($http,$q,$rootScope){
  return {
    startNewCampaign : function(){

      var e = $("input#email");
      var email = e.val();
      var campname = $("input#campaignname").val();
      var about = $("textarea#about").val();
      var tamt = $("input#targetamount").val();
      var edate = $("input#enddate").val();
      var invitees = $("input#invitees").val();
      var file_data = $("#file").prop("files")[0];
      var form_data = new FormData();     

      form_data.append("file",file_data);
      form_data.append("email",email);
      form_data.append("campaignname",campname);
      form_data.append("about",about);
      form_data.append("targetamount",tamt);
      form_data.append("enddate",edate);
      form_data.append("invitees",invitees);

      console.log(email+about+campname);

      var deferred = $q.defer();

      $http({
           method:'POST',
           url:'/startcampaign',
           data:form_data,
           headers:
             {
               'Content-Type'​ :'application/x-www-form-urlencoded'
             }
        }).success(function(data,status,headers,config) { 
          $rootScope.$apply( function() { 
          deferred.resolve(); 
        });
     }).error(function(){
        $rootScope.$apply(function() 
          { 
            deferred.reject();
          }); 
     });
     return deferred.promise;
   }
});
gvlasov
  • 18,638
  • 21
  • 74
  • 110
rohit
  • 83
  • 1
  • 2
  • 14

4 Answers4

2

Try something like this :

.service('fileUpload', ['$http', function ($http) {
  this.uploadFileToUrl = function(file, uploadUrl){
    var fd = new FormData();
    fd.append('file', file);
    $http.post(uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
    })
    .success(function(){
    })
    .error(function(){
    });
  }
}]);

Refer this link for explanation :

http://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs

Isha Aggarwal
  • 432
  • 5
  • 13
  • 1
    Please refrain from posting a link-only answer since the link might be unavailable someday. Instead, you could digest the content and answer with your own words, and then cite the link as a reference. – Tay2510 Apr 07 '15 at 07:32
  • @Tay2510 : Made the changes. Thanks for your feedback. :) – Isha Aggarwal Apr 27 '15 at 07:55
0

I had no luck with this either, and ending up using jQuery.ajax (although I was using jsonp).

See How can I post data as form data instead of a request payload?

Perhaps "And the data passed should be converted to a string" is relevant.

Community
  • 1
  • 1
peterorum
  • 1,401
  • 2
  • 15
  • 21
0

data in jQuery's $.ajax does not correspond to datain Angular's $http.get/post. You may have to use params instead of data in Angular. Have you looked at this:

jQuery ajax request works, same AngularJS ajax request doesn't

Community
  • 1
  • 1
Miichi
  • 1,739
  • 1
  • 14
  • 16
0

the problem is occurring due to content type, remove the headers part or add

    contentType:false or 'multipart/form-data'

      $http({
        method:'POST',
        url:'url'
        data:formData,
        contentType:false,
        processData: false
    }).
    then(function(result) {
        alert(result);
    });
Satish
  • 537
  • 2
  • 9
  • 21
  • No, this code didn't work. Content-Type should be in headers (`headers:{ 'Content-Type': '...'} ` ) in angularjs. Also i'm not sure about processData. – Morteza Jan 18 '16 at 14:07