Angular REST在JSON解析时返回语法错误

时间:2014-07-11 15:38:07

标签: angularjs rest

我收到错误"语法错误JSON解析json data"的第1行第1列的意外数据结尾。 RESTful服务正在运行,直接测试返回有效的json数据(在http://jsonlint.com/验证)

     [{"id":2,"name":"Flourescent Penetrant Inspection","description":"The fluorescent penetrant inspection process.","abbreviation":"FPI","tempId":null,"processType":"INSPECTION","indicationTypes":[{"id":1,"name":"Crack","description":"An identified crack on the piece.","abbreviation":"","tempId":null,"groupName":"","markupType":"LINE","useSizeDescription":true,"sizeDescription":"<= 1 in.::> 1 in.","rejectReason":"Crack","defaultDisposition":"MRB"},{"id":2,"name":"Inclusion","description":"An identified inclusion on the piece.","abbreviation":"","tempId":null,"groupName":"","markupType":"DOT","useSizeDescription":false,"sizeDescription":"","rejectReason":"Inclusion","defaultDisposition":"REWORK"}]},{"id":4,"name":"CMM","description":"The CMM process.","abbreviation":"CMM","tempId":null,"processType":"INSPECTION","indicationTypes":[]}]

HTTP响应中的错误,但它返回了200条消息。角应用程序将其视为空数组。有什么想法吗?

财务主任的适用部分是:

    indicationTypeController.constant("indicationTypeUrl", "http://localhost:8080/services/api/indication-types.json");

indicationTypeController.controller('indicationTypeController', function ($scope, $http, $resource, indicationTypeUrl) {

    $scope.indicationTypeResource = $resource(indicationTypeUrl+":id", { id: "@id" },
        { 'create': {method: "POST"}, 'update': {method: "PUT"}
        });

    $scope.listIndicationTypes = function () {
        $http.get(indicationTypeUrl)
            .success(function(data){
                $scope.indicationTypes = data;
            });

        //$scope.indicationTypes = $scope.indicationTypeResource.query();

    }
      . . . .

如您所见,我目前正在使用$ http.get,我也尝试过查询()。

任何

2 个答案:

答案 0 :(得分:0)

通常,$ http promise会返回一个包含标题和数据的对象。在$ http的成功处理程序中,您有

    $http.get(indicationTypeUrl)
        .success(function(data){
            $scope.indicationTypes = data;
        });

我非常确定data是完整响应,您需要使用此对象的data属性来获取特定数据。因此,这将成为以下内容:

    $http.get(indicationTypeUrl)
        .success(function(data){
            $scope.indicationTypes = data.data;
        });

在其他实现中,代替传入的参数被称为data,它通常被称为result,因此您可以引用包含的数据,如result.data而不是data.data

要确保的另一件事是在服务器和客户端之间正确设置Content-Type。如果不是application\json,您可能会遇到问题。

答案 1 :(得分:0)

这是一个CORS问题,请在发送结果之前将以下内容添加到响应标头中。

  

“Access-Control-Allow-Origin”:“*”

例如,如果您使用播放服务器(Java代码)来提供请求,则应将以下语句添加到您从中返回数据的方法

  

response()。setHeader(“Access-Control-Allow-Origin”,“*”);