如何将内容作为application / x-www-form-urlencoded发布

问题描述 投票:2回答:1

angularjs $ http.post拒绝使用我的Content-Type

我正在与一个承包商合作 - 他们正在制作服务器端API,而我正在使用angularjs整理一个javascript应用程序。他们坚持让api只允许使用application/x-www-form-urlencoded调用来调用,所以我试图弄清楚如何使用$ http进行urlencoded调用,并遇到问题。我发现的所有指令页面似乎都集中在旧版本的angularjs上。

我尝试使用以下代码:

$scope.makeApiCall = function(  ){
    var apiData = {
                    "key1" : "value1",
                    "key2" : "value2"
                };
    var apiConfig = {
                    "headers" : {
                        "Content-Type" : "application/x-www-form-urlencoded;charset=utf-8;"
                    }
                };
    return $http.post('/Plugins/apiCall', apiData, apiConfig)
    .then(function(response){
        $scope.data=response.data;
    });
};

但是当我拨打电话时,开发人员工具报告它使用的是Content-Type: text/html; charset=utf-8,而不是使用我提供的Content-Type。

如何让我的$http.post发送正确的内容类型?

angularjs api angularjs-http
1个回答
3
投票

如何将内容发布为application/x-www-form-urlencoded

使用$httpParamSerializerJQLike服务转换数据:

.controller(function($http, $httpParamSerializerJQLike) {
  //...

  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });

});

有关更多信息,请参阅

© www.soinside.com 2019 - 2024. All rights reserved.