Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am using $http to POST an angularjs variable, I do receive the JSON equivalent in servlet's doPost method. When I write a JSON in the response from the servlet, $http.success(data, status, header, config) is called. But when I assign 'data' to an angularjs variable, no value get's assigned to the variable. Tried using JSON.parse(data) but of no use. Following is the code snippet.

AngularJS

$scope.result = {
    title: "",
    subject: "",
    summary: "",
    body: "",
    extra: ""
};

$http.post(url, JSON.stringify(obj)).
success(function(data, status, headers, config){
        $scope.result = JSON.parse(data);
        console.log("data: " + data);
        console.log("status: " + status);
        console.log("headers: " + headers);
        console.log("config: " + config);
    })
    .error(function(data, status, headers, config){
        $scope.result = JSON.parse(data);
        console.log("data: " + data);
        console.log("status: " + status);
        console.log("headers: " + headers);
        console.log("config: " + config);
    });

Console output on receiving the response is as follows

data: [object Object]
status: 200 
headers: function (name) {
   if (!headersObj) headersObj =  parseHeaders(headers);

   if (name) {
      return headersObj[lowercase(name)] || null;
   }

   return headersObj;
  }
config: [object Object]

Following is the code snippet from the servlet.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        System.out.println("doPost called");
        response.getWriter().write("{"title":"Transaction Status","subject":"Congratulations!!","summary":"Successfully received your request, below is your Transaction ID","body":"5a85fc3b303d47dca6514b707442f2cd","extra":"Please save this transaction ID for future reference"}");
    }

I am not able to obtain the JSON sent from doPost() method in angularjs success function. Is it the correct way of doing it? Angularjs automatically converts the received JSON to javascript object type, so I must be able to assign 'data' straight to $scope.result.

It will be great if someone can please point me in right direction.

share|improve this question
    
data is an object containing a property data. That property holds what you want. You may want to rename data to something like response. If you do, you could assign response.data (without using JSON.parse() –  Sprottenwels Jul 23 '14 at 8:49
    
I tried your recommendation, following is the console output. response: undefined status: 200 headers: function (name) { if (!headersObj) headersObj = parseHeaders(headers); if (name) { return headersObj[lowercase(name)] || null; } return headersObj; } common.js:113 config: [object Object] –  Prakash Jha Jul 23 '14 at 8:57
    
In your success function, add console.dir(data) and add the response to your question please –  Sprottenwels Jul 23 '14 at 8:58
    
console.dir printed out the entire object and this is what I found in it. "body: "cb309b8454464a0e90b4aea3ecb7372c" extra: "Please save this transaction ID for future reference" subject: "Congratulations!!" summary: "Successfully received your request, below is your Transaction ID" title: "Transaction Status" Million thanks for the help, it solved my problem. –  Prakash Jha Jul 24 '14 at 7:12

1 Answer 1

Use this:

$http.post(url, $.param(obj), {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
})

If you do not want to use jQuery $.param, you can use an alternative

$http.post(url, obj, {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },
    transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    }
})
share|improve this answer
    
May I ask what that is supposed to do? –  Sprottenwels Jul 23 '14 at 8:55
    
To send data as form data instead of request payload –  Raghav Jul 23 '14 at 9:00
    
Which method is queryparams referencing? –  Eric Apr 4 at 2:26
    
Edit: queryparams was a mistake –  Raghav Apr 4 at 6:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.