2014-11-16 5 views
0

ASP .NET MVC에서 JsonResult을 반환하는 컨트롤러 메서드를 호출하고 있습니다.JSON.parse - 유효하지 않은 토큰

var result = JSON.parse(data); 

data이 인 경우 : 내 자바 스크립트에서

, 나는이 할 노력하고있어

{ 
    "ExtensionData": {}, 
    "ErrorMessage": "Test error message.", 
    "From": null, 
    "Notation": null, 
    "To": null, 
    "Valid": false 
} 

을하지만 다음과 같은 오류 얻을 :

SyntaxError: Unexpected token o at Object.parse (native)

I을 http://jsonlint.com/에있는 데이터를 구문 분석하려고했지만 거기에서 OK 구문을 분석합니다.

내가 뭘 잘못하고 있니?

편집 : 나는 AngularJS와를 사용하고 있는데이 같은 JSON을 반환하는 컨트롤러 메서드를 호출하고

.

$scope.move = function (to) { 
    var x = $http({ 
     url: moveUrl, 
     method: "POST", 
     params: { gameId: $scope.game.id, fromPosition: $scope.game.board.selectedSquare, toPosition: to } 
    }); 

    x.success(function (data) { 
     var moveResult = JSON.parse(data); 
    ... 
    ... 
    }); 

위와 유사한 호출이 실제로 정상적으로 작동합니다.

$scope.getGameModel = function() { 
    var x = $http({ 
     url: gameModelUrl, 
     method: "POST", 
     params: { gameId: $scope.game.id, startingFen: $scope.startingFen } 
    }); 

    x.success(function (data) { 
     $scope.game = JSON.parse(data); 
    }); 

    x.error(function (data) { 
     $scope.error = data; 
    }); 
+0

AJAX 통화에 추가 할 수 있습니까? – TysonWolker

+0

console.log (typeof data === 'object') 사실입니까? – TysonWolker

+1

반환하는 JsonResult (예 :'return Json (myObject);') 인 경우'data'는 이미 JSON이므로 다시 구문 분석하지 않아야합니다. –

답변

0

직접 JSON으로 변환 할 필요가 없습니다

var errorMessage = data.ErrorMessage; 

로 javascipt에 사용할 수 있습니다.

관련 문제