0

I'm making a method that when a person's name on a website is clicked, a request is triggered by using angularJS. The request should send the name as a string to the backend part of the app, that connects to an LDAP and extracts the info about the person using JSONobjects and then sends them back to the frontend part. I'm sure everything works because i tested it, the only problem i have is that i dont know how to pass the String value of the persons name to the backend request for the method. Here is the JS code that is triggered when the person's name is clicked:

var personApp = angular.module('personApp', [ 'ngRoute', 'ngCookies' ]);

personApp.controller('PersonController', function($scope, $http) {

    $scope.showPerson = function(name){
        $scope.name = name;


        $http({
            method : "POST",
            url : "AMPServlet",
            params : {
                parameterType : "personName",
                name:name

            }

        }).success(function(response) {
            $scope.personLDAP = response.person;
            console.log(response);
        });

    };

And here is the code of the servlet that calls the LDAP method and returns the info back to the JS part and then the front.

case "personName": {
    log.info("primljen request za dohvacanje informacija o dezurnim osobama");
    PersonADInfo info = new PersonADInfo();
    try {
        info = ldapAuthenticationServiceAccess.retrievePersonADInfo(session.getAttribute("ng-click").toString());
    } catch (NullPointerException e) {
        e.printStackTrace();
        log.error(e);
    }
    jsonObject = new JSONObject();
    tmpJSONObject = new JSONObject();
    try {
        tmpJSONObject = new JSONObject();
        tmpJSONObject.put("name", info.getName());
        tmpJSONObject.put("email", info.getEmail());
        tmpJSONObject.put("department", info.getDepartment());
        tmpJSONObject.put("workplace", info.getWorkplace());
        tmpJSONObject.put("phone", info.getPhone());
        tmpJSONObject.put("mobile", info.getMobile());
        tmpJSONObject.put("vpn", info.getVPN());
        tmpJSONObject.put("mobilevpn", info.getMobileVPN());
        jsonObject.put("person", tmpJSONObject);

    } catch (JSONException e) {
        e.printStackTrace();
        e.getMessage();
        log.error(e.getStackTrace());
    }

    writer.println(jsonObject.toString());

}
break;

The part i need help with is this line of code:

info = ldapAuthenticationServiceAccess.retrievePersonADInfo(session.getAttribute("ng-click").toString());

To be exact, i'm unsure of what to put inside od getAttribute() to get the name of the person to be given to the method as a parameter. Any help is appreciated, thanks!

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Marin
  • 11
  • 2
  • What makes you say that that line is the problem? To me it looks like it could be multiple other things. Could you show the HTML around the name that you're looking for and/or how the `showPerson` function is called? – Protozoid Jan 07 '19 at 13:26
  • 1
    I figured it out, i was calling session.getAttribute instead of request.getParameter for the name to be passed. Everything works, thanks anyway! – Marin Jan 07 '19 at 14:49
  • Well done and good luck :) – Protozoid Jan 07 '19 at 16:07
  • The [`.success` method is deprecated](https://stackoverflow.com/questions/35329384/why-are-angularjs-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). Avoid using it in new code. – georgeawg Jan 07 '19 at 17:38
  • With POST requests it is better to send data in the body of the request instead of using URL parameters. – georgeawg Jan 07 '19 at 17:44

0 Answers0