2016-08-10 5 views
0

우리는 여전히 어떻게 각도 JS 작품 배우고 각 $의 HTTP에서 정의되지 않는다, 우리가 약간의 문제, 우리는 header('Content-Type:application/json;'); 및 JSON으로 출력이 각PHP의 JSON 응답이

var app = angular.module('app',['ngMessages']); 
app.controller('AppCntrl', function($scope, PostingService) { 
    $scope.submitData = function() { 
    var data = { 
     name: "abc", 
     email: "[email protected]" 
    }; 
    PostingService.postData(data); 
    } 
}); 
app.service('PostingService', ['$http',function($http) { 
    this.postData = function(data) { 
    $http.post(
     '/url/to/post/request', 
     data 
    ) 
     .then(function successCallback(response) { 
     alert(response); // alerts undefined *********************** 
    }, function errorCallback(response) { 
     alert(response); // alerts undefined *********************** 
    }); 
    } 
}]); 

/url/to/post/request 동안과 http POST 요청을하고 있습니다 응답은 {"status":true,"message":"this is message"}과 비슷하지만 경고는 정의되지 않음을 나타냅니다.

답변

0

몇 가지 사항을 변경했지만 중요하지 않습니다. 이 코드가 작동하는 것 같습니다. > 변수 requestURI

var app = angular.module('app', []) 
 

 
app.controller('AppCtrl', function($scope, PostingService) { 
 
    $scope.submitData = function() { 
 
    var data = { 
 
     name: 'abc', 
 
     email: '[email protected]' 
 
    } 
 
    PostingService.postData(data) 
 
    } 
 
}) 
 

 
app.service('PostingService', ['$http', function($http) { 
 
    var requestURI = '' 
 
    this.postData = function(data) { 
 
    $http.post(requestURI, data) 
 
     .then(function(response) { 
 
     alert(response) 
 
     }, function(response) { 
 
     alert(response) 
 
     }) 
 
    } 
 
}])
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
    </head> 
 
    <body ng-app="app"> 
 
    <div ng-controller="AppCtrl"> 
 
     <button ng-click="submitData()">Make POST Request</button> 
 
    </div> 
 
    </body> 
 

 
</html>

변경