2013-08-28 2 views
0

localhost : 3001을 실행하는 노드 서버에 게시물 요청을 보내야합니다. 요청을 성공적으로 완료하고 노드 서버에서 게시물 데이터를 가져 오는 중 데이터 형식이 잘못되었습니다.AngularJS 및 ExpressJS를 사용하여 응답 JSON 데이터

AngularJS와 :

function MyCtrl1($scope,$http,$location) {  
    $scope.user = { }; 
    $scope.login = function() { 

    $http.post("http://localhost:3001/login", 
       $scope.user, 
       {'Content-Type': 'application/json'}).success(function(result) { 
        $scope.resultPost = result; 
        $location.path('/'); 

       }).error(function() { 
        console.log("error"); 
       }); 
    }; 
} 

Nodejs :

app.post('/login', function(req,res) { 
    console.log(JSON.stringify(req.body)); 

    res.end('ok'); 
}); 

log : {"{\"username\":\"test\",\"password\":\"pass123\"}":""} 

여기 형식의 데이터를 얻을 수있는 방법이있다?

+0

JSON.parse (str); – Dylan

답변

1

JSON.parse()를 사용하여 본문을 JavaScript 객체로 구문 분석 할 수 있습니다.

var obj = JSON.parse(req.body); 
var username = obj.username; 
var password = obj.password; 
+0

네의 작업, 지금은 점점 { '{ "사용자 이름", "비밀번호", "테스트": "pass123"}': ''} 을하지만 난 그것에서 사용자 이름과 암호 .. – Nish

+0

의 가치를 얼마나 @ Nish 객체로 성공적으로 변환 된 경우 점 개념을 사용하여 필드에 액세스 할 수 있습니다. 답변을 업데이트했습니다. – zsong

+0

노드 서버를 변환하려고하면 오류가 발생합니다. SyntaxError : 예기치 않은 토큰 o Object.parse (native)에 이 있습니다. – Nish