2016-09-09 2 views
0

Я нашел фрагмент кода для использования AngularJS с Java Servlet.AngularJS и Java Servlet не работают

Проблема в том, что когда я запускаю приложение, он говорит мне, что страницы «AtpPlayers» она не существует и, конечно же, она не заполняет мою таблицу.

Я пытался использовать сервер JBOSS и библиотеку json-simple-1.1.1.jar.

Что делает неправильно?

сервлет здесь:

import java.io.IOException; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@WebServlet("/AtpPlayers") 
public class AtpPlayers extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     response.setContentType("application/json;charset=UTF-8"); 
     try { 
      PrintWriter out = response.getWriter(); 
      out.println("[{\"name\": \"Nadal, Rafael (ESP)\", \"email\": \"[email protected]\", \"rank\": \"1\"}," 
       + "{\"name\": \"Djokovic, Novak (SRB)\", \"email\": \"[email protected]\", \"rank\": \"2\"}," 
       + "{\"name\": \"Federer, Roger (SUI)\", \"email\": \"[email protected]\", \"rank\": \"3\"}," 
       + "{\"name\": \"Wawrinka, Stan (SUI)\", \"email\": \"[email protected]\", \"rank\": \"4\"}," 
       + "{\"name\": \"Ferrer, David (ESP)\", \"email\": \"[email protected]\", \"rank\": \"5\"}]"); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    } 
} 

HTML-& JS код является то, что:

<!DOCTYPE html> 
<html> 
    <head>  
     <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>  
    </head> 
    <body ng-app="ATP_PLAYERS"> 
     <div ng-controller="atpController"> 
      <h5>Loading ATP players from a Servlet</h5> 
      <table> 
       <thead> 
        <tr> 
         <th>Rank</th> 
         <th>Name</th> 
         <th>E-mail</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr ng-repeat="item in atp"> 
         <td>{{item.rank}}</td>    
         <td>{{item.name}}</td> 
         <td>{{item.email}}</td> 
        </tr> 
       </tbody> 
      </table> 
     </div>  

     <script language="javascript" type="text/javascript"> 
      angular.module('ATP_PLAYERS', []) 
      .controller('atpController', function ($scope, $http) { 
       $http.get('http://localhost:8080/AngularGET/AtpPlayers').then(function (data, status, headers, config) { 
       $scope.atp = data; 
       alert('OK'); 
      }, function (data, status, headers, config) { 
       alert('NU'); 
       }); 
     }); 
     </script> 
    </body> 
</html> 
+0

Достигнул ли сервер? что вы получаете, когда попадаете в URL в браузере? –

+0

Теперь FireBug сообщает мне «200 OK» и ошибка Ошибка: «JSON.parse: ожидаемое имя свойства или«} »в строке 1 столбца 3 данных JSON oc @ http: //ajax.googleapis.com /ajax/libs/angularjs/1.3.8/angular.min.js:14:151 .... " – Doro

+0

И ответ таков: [{'name': 'Nadal, Rafael (ESP)', 'email' : '[email protected]', 'rank': '1'}, {'name': 'Djokovic, Novak (SRB)', 'email': '[email protected]', 'rank': ' 2 '}, {' name ':' Federer, Roger (SUI) ',' email ':' federerroger @ gmail.com ',' rank ':' 3 '}, {' name ':' Wawrinka, Stan (SUI) ',' email ':' [email protected] ',' rank ' :' 4 '}, {' name ':' Ferrer, David (ESP) ',' email ':'[email protected] ',' rank ':' 5 '}] – Doro

ответ

0

Я нашел проблему:

$http.get('http://localhost:8080/AngularGET/AtpPlayers').then(function (data, status, headers, config) { 
    $scope.atp = data; 
    alert('OK'); 
}, function (data, status, headers, config) { 
    alert('NU'); 
}); 

заменен:

$http.get('http://localhost:8080/AngularGET/AtpPlayers').then(function (response) { 
    $scope.atp = response.data; 
    alert('OK'); 
}, function (response) { 
    alert('NU'); 
}); 

Я не знаю, почему парень, который написал исходный код, набрал «функция (данные, статус, заголовки, конфиг)» ...

Смежные вопросы