gpt4 book ai didi

javascript - angularJs , $http woocommercce 设置默认参数

转载 作者:行者123 更新时间:2023-11-28 05:34:45 26 4
gpt4 key购买 nike

我了解了 js 和 angularjs ,为了测试我的知识,我尝试从 woocommerce(wordpress) 获取数据,所以我找到了 api reernce: https://woothemes.github.io/woocommerce-rest-api-docs/?shell#authentication-over-https

curl https://example.com/wp-json/wc/v1/products \
-u consumer_key:consumer_secret

代码:

myApp.controller('homeCtrl',['$scope','$http',function($scope,$http){

console.log('App Start')
var secretKey = {
consumer_key: 'ck_bc51576e596ae1fbf535f8a3d60b281541407006',
consumer_secret: 'cs_44beee13f3eef60a9d5fb66142fc40a3ba1d2989'

}

$http({ method: 'GET',
url:'https://www.ng-il.com/shop/wp-json/wc/v1/products',
params: secretKey
}).then(function(res){
console.log(res)
})

}]);

它工作正常,我的问题:

1)我如何为所有请求(angularJs)设置此参数?

2)在js(没有AngularJs)中传递参数只能通过var.send(params)?

3)这是我需要传递counsumer_key&consumer_secret的方式?

或者还有其他办法吗?

感谢大家!

最佳答案

要将这些参数传递给您的所有请求,您可以使用 interceptors :

var myApp = angular.module('myApp', []);

myApp.config(function ($httpProvider) {
$httpProvider.interceptors.push(function ($q) {
return {
'request': function (config) {
config.params = {
consumer_key: 'ck_bc51576e596ae1fbf535f8a3d60b281541407006',
consumer_secret: 'cs_44beee13f3eef60a9d5fb66142fc40a3ba1d2989'
};
return config;
}

}
});
});

关于javascript - angularJs , $http woocommercce 设置默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39438305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com