【发布时间】:2014-09-17 18:19:23
【问题描述】:
这是我的 Angular 代码(获取请求工作正常,但不是发布请求。我在 Servlet 端的 carrier_id 中收到 null)。
app.js
var app = angular.module( "admin" , []);
app.config(function ($httpProvider){
// set the Content-Type header globally
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// define ajax call behavior globally
// set up global transformRequest function
$httpProvider.defaults.transformRequest = function(data){
if (data === undefined) {
return data;
}
return $.param(data);
};})
app.controller('mainController', function($scope, $http, $window){
$scope.fileList =[];
$scope.selectedFile = null;
$http({
method: 'GET',
url :'hrp/filelist'
}).success( function(data,status,headers,config){
data.file_list.splice(0, 1) // to remove header row
$scope.fileList=data.file_list
}).error(function(){
alert("error")
})
$scope.submitFile = function(selectedFile){
console.log("happy" + selectedFile);
$http({
method: 'POST',
url: 'hrp/carrierInfo',
data: {carrier_id: selectedFile}
}).success(function(data){
alert(data.carrier_id + " & " + data.method_name)
}).error(function(){
alert("errors")
})
}});
以及部分 Servlet 代码:
String carrier_id = (String)request.getAttribute("carrier_id");
response.setContentType("application/json");
但它是空的。
【问题讨论】:
-
您发送的内容类型是 application/json 还是 form-encoded?
-
是的,response.setContentType("application/json");