使用angular not working

时间:2017-01-11 11:03:16

标签: javascript html angularjs

我试图注入json表单wordpress插件" JSON-API"。 换句话说,我只是想在我的网站上制作新闻小组,但我希望让我的生活更轻松,并在简单的所见即所得编辑上发帖。

HTML:

<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
        <script src="maincontroller.js" type="text/javascript"></script>
    </head>
    <body bgcolor="lightblue">
        <div ng-app="mainApp" ng-controller="mainController">
            <div id="content">
                <div ng-repeat="content in contents">
                    <h2>{{content.title}}</h2>
                    <p>{{content.date}}</p>
                    <p>{{content.content}}</p>
                    <p>{{content.url}}</p>
                </div>
            </div>
        </div>
    </body>
</html>

mainController.js:

var myapp=angular.module('mainApp',[]);
myapp.controller('mainController',function($scope,$http){

    $http({method: 'GET', url: 'http://192.168.0.16/wordpress/?json=1'}).
        then(function(data, status, headers, config) {
            $scope.contents=data[0];
        }).
        error(function(data, status, headers, config) {  
             $scope.contents = [{heading:"Error",description:"JSON invalid"}];        
    });
});

不幸的是,没有任何内容只显示控制台中的错误

angular.js:14328TypeError: $http(...).then(...).error is not a function
    at new <anonymous> (maincontroller.js:9)
    at Object.invoke (angular.js:4842)
    at R.instance (angular.js:10695)
    at n (angular.js:9572)
    at g (angular.js:8881)
    at angular.js:8746
    at angular.js:1843
    at m.$eval (angular.js:17972)
    at m.$apply (angular.js:18072)
    at angular.js:1841

任何想法的人?

3 个答案:

答案 0 :(得分:0)

请将then更改为success

.success(function(data, status, headers, config) {
            $scope.contents=data[0];
        })

答案 1 :(得分:0)

您错误地使用了then

.then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

您可以按$http({method: 'GET', url: 'http://192.168.0.16/wordpress/?json=1'})

最小化$http.get('http://192.168.0.16/wordpress/?json=1')

使用以下代码替换您的控制器

var myapp=angular.module('mainApp',[]);
myapp.controller('mainController',function($scope,$http){
    $http.get('http://192.168.0.16/wordpress/?json=1').then(function(response){
        $scope.contents = response[0];
    }, function(error){
        $scope.contents = [{heading:"Error",description:"JSON invalid"}]; 
    })
});

答案 2 :(得分:0)

 $http.get("url",{}).
     then(function(response){
         alert("success");
     }, function myError(response) {
            alert("error");
        });
 try this hope it should work.