使用Angular

时间:2016-02-10 14:27:05

标签: angularjs wordpress cordova ionic-framework woocommerce

我正在Cordova平台上的Angular / Ionic项目中实现Woo Commerce Rest API。当我发出$ http请求获取产品列表或任何其他数据时,我收到错误消息。这是我的服务代码:

angular.module('services.serverRepo', [])
.service("serverRepo", 
['$q','$http','errorHandler','$ionicLoading',function($q,$http,errorHandler,$ionicLoading){

var baseUrl="www.abc.com/wc-api/";
var self=this;
this.products=function(){
    var deff=$q.defer();
    $http({
        method:"GET",
        url:baseUrl+"v3/products",
        headers: {
                    "Content-Type":"application/JSON",
                    "oauth_consumer_key":"gjdfjkbgbdhh645h6bh456b45hbhbgdfhgbdfhgbdfhgbhgbdhfghhfhf",
                    "consumer_secret":"cs_97d74bbf5e9052ee053a05cbb1a53eec19c0847c"
                }
        }).then(function(objS){
            alert('Success :-    '+JSON.stringify(objS));
        },function(objE){
            alert('error:-    '+objE);
            errorHandler.serverErrorhandler(objE);
            deff.reject("server Error");
        });
        return deff.promise;
    };
}])
.service('errorHandler',['$q',function($q){
    this.serverErrorhandler=function(error){
        alert("ERROR ::"+JSON.stringify(error));
        console.log("ERROR ::"+JSON.stringify(error));
    };
 }
])

在我的controller.js文件代码中是:

$scope.rentaldeptt = function(){
    //$ionicHistory.clearCache();
        serverRepo.products().then(function(objS){

                                   },function(err){
        });
}

我在点击按钮时调用$scope.rentaldeptt。作为回应我收到错误消息

  

{"数据" {"错误":[{"代码":" woocommerce_api_authentication_error""消息&#34 ;:" oauth_timestamp参数缺失"}]},"状态":404," config":{"方法":&#34 ; GET"" transformRequest":[空]," transformResponse":[空]," URL":" www.abc.com / v3 / products"," header":{" Accept":" application / json,text / plain, / &#34 ;}" PARAMS" {" oauth_consumer_key":" gjdfjkbgbdhh645h6bh456b45hbhbgdfhgbdfhgbdfhgbhgbdhfghhfhf"" CONSUMER_SECRET":" cs_97d74bbf5e9052ee053a05cbb1a53eec19c0847c"}} ," statusText":" Not Found"}

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:-1)

请尝试按照以下步骤解决问题,

在这里,我已经在angularjs中创建了服务来处理使用oauth调用woocommerce api,

            angular.module('myapp.restservices', [])
            .service("serverRepo",['$q','$http','errorHandler','$ionicLoading',function($q,$http,errorHandler,$ionicLoading){
                var self=this;
                 //Request Url and method
                var request = {
                        url: 'http://www.example.com/wc-api/v3/products',
                        method: 'GET'
                };  
                //OAuth Protocol authentication parameters
                var oauth = new OAuth({
                        consumer: {
                            //Consumer Public Key
                            public: 'ck_50xxxx',
                            //Consumer Secrete Key
                            secret: 'cs_b4xxxx'
                        },
                        //oauth1.0a protocol signature method
                        signature_method: 'HMAC-SHA1'
                });

                //Service Function to get products
                this.products=function(){
                     $ionicLoading.show({
                            template: '<ion-spinner class="light"></ion-spinner>'
                     });
                     //OAuth Parameters to call woocommerce api
                    var oauth_data = {
                        oauth_consumer_key: oauth.consumer.public,
                        oauth_nonce: oauth.getNonce(),
                        oauth_signature_method: oauth.signature_method,
                        oauth_timestamp: oauth.getTimeStamp()
                    };
                    //Oauth signature
                    oauth_data.oauth_signature = oauthSignature.generate(request.method,request.url,oauth_data,oauth.consumer.secret );
                    console.log("Params : "+ JSON.stringify(oauth_data));
                    var deff=$q.defer();
                    $http({
                        method:"GET",
                        url:request.url,
                        headers: {
                                    "Content-Type":"application/JSON",
                                },
                        params: oauth_data
                        }).then(function(objS){
                            $ionicLoading.hide();
                            alert('Success :-    '+JSON.stringify(objS));
                        },function(objE){
                            $ionicLoading.hide();
                            alert('error:-    '+JSON.stringify(objE));
                            errorHandler.serverErrorhandler(objE);
                            deff.reject("server Error");
                        });
                        return deff.promise;
                    };
                }])
                .service('errorHandler',['$q',function($q){
                    this.serverErrorhandler=function(error){
                        alert("ERROR ::"+JSON.stringify(error));
                        console.log("ERROR ::"+JSON.stringify(error));
                    };
                 }
            ])

写控制器以调用服务功能,如下所示,

    angular.module(myapp.categorycontrollers, [])
    .controller('MainCtrl', function($scope,woocommerce) {
            //Method to get all Products
              $scope.getAllProducts = function(){
                    woocommerce.products().then(function(objS){
                     },function(err){
                    });
              }
             //calling to function
              $scope.getAllProducts();
     }

希望这会对你有帮助!