简体   繁体   中英

sending FormData with angular $http

I'm writing an angular-1.5.0-rc0 application

In this application I create FormData, fill it with values and I need to send it with $http request. for now I use jquery ajax with the following code:

this.addDrink = function () {
        var data = new FormData();
        var drinkBrand = caller.drink.drinkBrand;
        var drinkType = caller.drink.drinkType;
        var drinkFlavor = caller.drink.drinkFlavor;
        var liquidColor = caller.drink.liquidColor;
        var drinkCompany = caller.drink.drinkCompany;
        var liquidIsTransparent = caller.drink.liquidIsTransparent;
        var drinkImage = caller.drink.drinkImage;
        var caloriesFor100g = caller.drink.caloriesFor100g;
        var alcoholSum = caller.drink.alcoholSum;
        var alcoholSumType = caller.drink.alcoholSumType;
        data.append('alcohol_sum_type', alcoholSumType);
        data.append('drink_brand', drinkBrand);
        data.append('drink_type', drinkType);
        data.append('drink_flavor', drinkFlavor);
        data.append('liquid_color', liquidColor);
        data.append('liquid_is_transparent', liquidIsTransparent);
        data.append('drink_image', drinkImage);
        data.append('drink_company', drinkCompany);
        data.append('calories_for_100g', caloriesFor100g);
        data.append('alcohol_sum', alcoholSum);
        $.ajax({
            url: 'https://api.myalcoholist.com:8888/drink',
            data: data,
            processData: false,
            contentType: false,
            type: 'POST',
            success: function (data) {
                if (!data.success) {
                    alert(data.data);
                } else {
                    alert('done');
                }
            }
        });
    };

as you can see I set processData and ContentType to false in order to be able to send FormData in jquery ajax call. how can it be done with $http?

$scope.addDrink = function(files) {

// append all what u want.

$http.post('https://api.myalcoholist.com:8888/drink', data, {       
    headers: {'Content-Type': undefined },
    transformRequest: angular.identity
}).success( ...all right!... ).error( ..damn!... );

};

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