AngularJS http POST to Servlet

后端 未结 3 1142
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 09:01

I went through a lot of StackOverflow answers and googled a lot but still could not find why my post request is not working.

This is my jsp:

相关标签:
3条回答
  • 2020-12-16 09:09

    You are using a POST request and your data is sent in the request body - not as parameter. You need to read the content using request.getReader().

    // example using the javax.json.stream package
    JsonParser parser = Json.createParserFactory().createParser(request.getReader());
    
    0 讨论(0)
  • 2020-12-16 09:12

    Your request does not contain a URL parameter named "data", therefore request.getParameter("data") returns null and you get the NullPointerException.

    You try to send a Javascript object via URL parameters which does not go well with non-shallow objects.

    I would recommend to send the data as request payload:

    JsonObject obj = (JsonObject) parser.parse(request.getReader());
    

    On the client you need to make sure that your data is sent as proper JSON:

    $http({
            method : 'POST',
            url : 'login',
            contentType: 'application/json',
            data : JSON.stringify($scope.user),
        })...
    
    0 讨论(0)
  • 2020-12-16 09:29

    i think you should be sending data as

    $http({
        method : 'POST',
        url : 'login',
        data : {data: $scope.user},
        headers: {
            'Content-Type': 'application/json'
        }
    }).success(function(data) {
        console.log(data);
    });
    

    pay attention to data : {data: $scope.user}

    0 讨论(0)
提交回复
热议问题