0

Here is my Angular Code(Get request is working fine but not Post request. I am receving null in carrier_id on Servlet side).

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

And part of Servlet Code:

String carrier_id = (String)request.getAttribute("carrier_id");

response.setContentType("application/json");

but it is null.

SoftEngineer1
  • 57
  • 1
  • 10

1 Answers1

1

You may need to set the content type:

try form-urlencoded

$http({
    method: 'POST',
    url: 'hrp/carrierInfo',
    data: {carrier_id: selectedFile},
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(function(result) {
       console.log(result);
   }, function(error) {
       console.log(error);
   });

and try application/json

$http({
    method: 'POST',
    url: 'hrp/carrierInfo',
    data: {carrier_id: selectedFile},
    headers: {
        'Content-Type': 'application/json'
    }
}).then(function(result) {
       console.log(result);
   }, function(error) {
       console.log(error);
   });

see if the server gets different results

SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98