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

标签 angularjs api angularjs-http

angularjs $http.post 拒绝使用我的内容类型

我正在与一个承包商合作 - 他们的团队正在制作服务器端 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; 而不是使用我提供的 Content-Type。字符集=utf-8

如何让我的 $http.post 发送正确的 Content-Type?

最佳答案

如何将内容发布为 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'
    }
  });

});

欲了解更多信息,请参阅

关于angularjs - 如何将内容发布为 application/x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55032377/

相关文章:

javascript - 延迟 promise 值未更新/解析/延迟

javascript - 从 Angularjs 客户端在 POS 上打印

python - 在 google app engine 上使用 python [xmlrpc frontend] 模块连接到 WordPress 博客

python - 推特(Tweepy): What counts as a search API call?

javascript - 从 $http GET 服务返回函数值

javascript - 如何从 AngularJS $http 请求中获取实际错误?

javascript - 正则表达式性能问题

javascript - 在 md-autocomplete 上使用 ng-blur?

c# - 基础连接已关闭。发送时发生意外错误

angularjs - 获取 AngularJS 错误 : "[$rootScope:inprog] $digest already in progress" without a manual $apply