使用角$ http发送FormData

时间:2015-12-28 06:22:40

标签: javascript angularjs angular-http

我正在写一个angular-1.5.0-rc0应用程序

在这个应用程序中,我创建FormData,用值填充它,我需要用$ http请求发送它。现在我使用jquery ajax,代码如下:

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

如您所见,我将processData和ContentType设置为false,以便能够在jquery ajax调用中发送FormData。如何使用$ http?

1 个答案:

答案 0 :(得分:2)

[ -n $var1; ]