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/