【问题标题】:angular js fails to parse jax-rs responseangular js 无法解析 jax-rs 响应
【发布时间】:2017-10-19 11:27:14
【问题描述】:

我希望我的服务器向 Angular js 客户端发送指示性错误。

我看到了这些例子:

Example 7
Project: spice   File: RequestService.java View source code 6 votes vote down vote up
@GET
@Path("date")
@Produces("text/plain")
public Response get(@Context Request request) {
    final Date modificDate = getLastModificationDateFromDatastore();
    final EntityTag entityTag = getEntityTagFromDatastore();
    final ResponseBuilder resp = request.evaluatePreconditions(modificDate,
            entityTag);
    if (resp != null) {
        return resp.build();
    }
    // Build new Response
    final ResponseBuilder responseBuilder = Response.status(200);
    responseBuilder.entity("This is the Entity from " + modificDate);
    responseBuilder.lastModified(modificDate);
    responseBuilder.tag(entityTag);
    return responseBuilder.build();
}

Example 8
Project: zeppelin   File: JsonResponse.java View source code    4 votes vote down vote up
public javax.ws.rs.core.Response build() {
  ResponseBuilder r = javax.ws.rs.core.Response.status(status).entity(this.toString());
  if (cookies != null) {
    for (NewCookie nc : cookies) {
      r.cookie(nc);
    }
  }
  return r.build();
}

但是当我的服务器代码发送时:

catch (Exception ex)
{

    String error = "RuntimeException: test11";
    logger.error("update voice filed. Error: "+error);

    return Response.serverError().entity(error).build();
}

在客户端代码中我找不到这个错误信息:

            $http.put('api/foo', {voice : voice, isAddMode : isAddMode}).then(
                function successCallback(response) {
..
                }, function errorCallback(response) {
                    var responseStr = JSON.stringify(response)
                    console.log(responseStr);
                    deferred.reject(responseStr);
                });

我得到解析错误,但一般我在 js 中找不到正确的成员。

SyntaxError: Unexpected token R in JSON at position 0
    at JSON.parse (<anonymous>)
    at fromJson (angular.js:1377)
    at defaultHttpResponseTransform (angular.js:11003)
    at angular.js:11094
    at forEach (angular.js:357)
    at transformData (angular.js:11093)
    at transformResponse (angular.js:11914)
    at processQueue (angular.js:16648)
    at angular.js:16692
    at Scope.$eval (angular.js:17972)

【问题讨论】:

  • Angular 正在尝试解析 RuntimeException: test11 这不是有效的 JSON

标签: javascript java angularjs jax-rs


【解决方案1】:

我不得不删除服务器端的"Produces"注解:

    @Path("/foo")
    @PUT
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
//    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")

然后从服务器发送一个 json 对象,而不仅仅是字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-12
    • 2015-06-19
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多