Search code examples
angularjsjsonjsonparser

Angularjs Unexpected token JSON


I have a problem with angularjs and JSON.

This is my code.

list_user.jsp (this is the page where I print the table)

<tr ng-repeat="per in persons">
  <td>{{ per.user }}</td>
  <td>{{ per.password }}</td>
  <td>{{ per.profile }}</td>
</tr>

controller.js

 $http({
      method : 'POST',
      url : 'views/user/user.json'
    }).success(function(data){
      $scope.persons = data;
    });

user.json

 [
        {
            "user": "Quarterback",
            "password": 5,
            "profile": "ppp"        
        },
        {
            "user": "Wide Receiver",
            "password": 89,
            "profile": "oooo"
        }
    ]

This way the table is generated correctly, but the json is fixed. Now I'll paste the code with which I want to print the data by taking them from a query

controller.js

 $http({
        method : 'POST',
        url : 'views/user/user.jsp'
    }).success(function(data){

        /*
        var jsonStr="your json string";
        var json=JSON.stringify(data);
        json=JSON.parse(json)
        console.log(json);
        */
        console.log(data);
        $scope.persons = data;
    });

The code between / * .. * / left them to show that I also tried that road without success.

user.jsp

JSONArray jdata = new JSONArray(); 
UserRest as = new UserRest();
jdata = as.getAll();
logger.info("jdata in user.jsp "+jdata);

UserRest.class (just paste the code where I create the JSON)

 while (iter.hasNext()) {
      User ut = (User) iter.next();
      JSONObject jtemp = new JSONObject();
      jtemp.put("user", ut.getUserName());
      jtemp.put("password", ut.getPassword());
      jtemp.put("profilo", ut.getProfilo());
      jarray.put(jtemp);
    }
    return  jarray;

the result of logger.info("jdata in user.jsp "+jdata) in user.jsp

jdata in user.jsp [{"user":"aaaaaaa","password":"1111111","profile":"0"},{"user":"bbbbbbbb","password":"222222222","profile":"1"}]

As you can see the json looks the same, but when in the browser I call the list_user.jsp page in the console the value "data" in controller.js returns me

 <? Xml version = "1.0" encoding = "utf-8"?>

I also tried with JSON.parse or JSON.stringify but it does not work. I also added "track by $ index" here:

<tr ng-repeat = "for people track by $ index">

in list_user.jsp but it does not work.

Please help me because I do not know how to do it anymore.


Solution

  • Well, your problem appears to be that you should call a SERVICE, not a jsp.

    A JSP builds a new web page for you and so it will put that:

    < ? Xml version = "1.0" encoding = "utf-8"? >
    

    Try doing, instead a jsp, a servlet (or use JAX-RS, as you prefer) and putting you logic inside the 'doGet' method (again, or use JAX-RS).

    Do a simple thing, like

     protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
    response.getWriter().append(your working JSON properly hardcoded);
    }
    

    This way should work. Then put your service logic there.