繁体   English   中英

AngularJS意外令牌JSON

[英]Angularjs Unexpected token JSON

我对angularjs和JSON有问题。

这是我的代码。

list_user.jsp(这是我打印表格的页面)

<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"
        }
    ]

这样表可以正确生成,但是json是固定的。 现在,我将通过从查询中获取数据来粘贴要用于打印数据的代码

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;
    });

/ * .. * /之间的代码使它们表明我也曾尝试过这条路,但没有成功。

user.jsp

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

UserRest.class(只需将代码粘贴到创建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;

user.jsp中logger.info(“ user.jsp中的jdata” + jdata)的结果

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

如您所见,json看起来一样,但是当我在浏览器中在控制台中调用list_user.jsp页面时,controller.js中的“ data”值返回了我

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

我也尝试使用JSON.parse或JSON.stringify,但是它不起作用。 我还在这里添加了“按$索引跟踪”:

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

在list_user.jsp中,但是它不起作用。

请帮助我,因为我不知道该怎么办了。

好吧,您的问题似乎是应该调用SERVICE,而不是jsp。

JSP为您构建了一个新的网页,因此它将包含以下内容:

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

尝试代替jsp来做一个servlet(或根据需要使用JAX-RS),然后将逻辑放入“ doGet”方法内(再次使用JAX-RS)。

做一个简单的事情,像

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

这种方法应该可行。 然后将您的服务逻辑放在这里。

非常感谢@inigoD ,我解决了我的问题。 遵循新代码。

controller.js(UserRest是servlet)

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

web.xml

<servlet>
        <servlet-name>UserRest</servlet-name>
        <servlet-class>"path application".UserRest</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserRest</servlet-name>
        <url-pattern>/UserRest</url-pattern>
    </servlet-mapping>

UserRest.class

@Override
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        try
        {
            JSONArray jdata = new JSONArray();
            jdata = getAll();

            logger.info("prima del response "+jdata);

            response.setContentType("application/json");
            response.getWriter().write(jdata.toString());

        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
    }

函数getAll()与我粘贴的相同,其中包含以下代码。

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;

我解决了servlet的问题,但是我想调用REST api。 我更喜欢不要使用servlet。 我更改代码。

controller.js

angular.module('inspinia')
.controller("DataTable",['$scope','$http',function($scope,$http){
$http({
        method : 'GET',
        url : '/api/utenti' 
    }).success(function(data){
        $scope.persons = data;
    });

web.xml

<servlet>
      <servlet-name>SpringServlet</servlet-name>
      <servlet-class>com.xxx.yyy.userInterface.servlet.SpringServlet</servlet-class>
      <load-on-startup>3</load-on-startup>
    </servlet>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet-mapping>
        <servlet-name>SpringServlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

UtentiController.class

@Path("/utenti")
@Component
@Scope("request")
public class UtentiController {

    @GET
    @Produces("application/json")
    public JSONArray getAll() {
       /* call the interface that return a json */

       return jarray;
    }
}

控制台中的错误是:

HTTP Status [404] – [Not Found]

Type Status Report

Message /api/utenti/

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

谢谢你的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM