rohit
rohit

Reputation: 83

Angularjs $http not sending form data to the server

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;
   }
});

Upvotes: 0

Views: 6361

Answers (4)

Isha Aggarwal
Isha Aggarwal

Reputation: 452

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

Upvotes: 2

Satish
Satish

Reputation: 537

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);
    });

Upvotes: 0

Miichi
Miichi

Reputation: 1799

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

Upvotes: 0

peterorum
peterorum

Reputation: 1421

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.

Upvotes: 0

Related Questions