纯 Angular Cloudant 连接
Pure Angular Cloudant connection
我正在尝试仅使用 angular 连接到我的 cloudant。第一个 post 成功,我能够使用第一个 get 方法从 cloudant 检索 document/view。
但是当我执行连续的 post 方法时,我收到了 401 未经授权的错误消息。有人能解释一下我在这里犯了什么错误吗?请详细回复,因为我是 angular 和 cloudant (RESTful arch)
的新手
var app = angular.module('myApp', []);
const ACCOUNTNAME = 'my acc';
const APIKEY = 'my key';
const APIPASSWORD = 'my pass';
const DATABASE = 'my db';
var cloudantDoc = 'link of document to update';
app.controller('tutorialCtrl', function($scope, $http) {
//Authentication header settings
var req = {
method: 'POST',
url: 'https://'+ACCOUNTNAME+'.cloudant.com/_session',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'name='+APIKEY+'&password='+APIPASSWORD,
withCredentials: true
}
$http(req).then(function(result){
//Grab data
req = {
method: 'GET',
url: 'https://'+ACCOUNTNAME+'.cloudant.com/'+DATABASE+'/_design/AllClients/_view/AllClients',
withCredentials: true
}
$http(req).then(function(result){
$scope.tableData = result.data;
console.log(result);
},
function(){
console.log("Failed at grabbing data");
});
},
function(){
console.log('Failed at authenticating');
}
);
$scope.revisionNumNews;
$http.post(cloudantDoc, JSON.stringify({"_id": "CLient1", "Client_Name":"ABC", "_rev": ""})).success(function(response){
revisionNumNews = response.rev;
});
在您的代码中有 var cloudantDoc = 'link of document to update'
,如果您尝试更新现有文档,则需要发出 PUT 请求而不是 POST。在任何情况下,您的请求都应包括 {withCredentials: true}
选项。
如果您正在尝试创建新文档,请尝试将您的 POST 请求更改为如下内容:
var databaseUrl = 'https://' + ACCOUNTNAME + '.cloudant.com/' + DATABASE
$http.post(databaseUrl, {'_id':'CLient1', 'Client_Name':'ABC'}, {withCredentials: true})
.then(function(response) {
$scope.revisionNumNews = response.rev
})
但是,如果您尝试更新现有文档,则需要发出 PUT 请求并包含文档的最新 _rev
。它看起来像这样:
var databaseUrl = 'https://' + ACCOUNTNAME + '.cloudant.com/' + DATABASE
$http.put(databaseUrl + '/' + cloudantDocId, cloudantDoc, {withCredentials: true})
.then(function(response) {
$scope.revisionNumNews = response.rev
})
其中 cloudantDocId
是正在更新的文档的文档 ID,cloudantDoc
是更新后的文档对象(最新的 _rev
)
我正在尝试仅使用 angular 连接到我的 cloudant。第一个 post 成功,我能够使用第一个 get 方法从 cloudant 检索 document/view。
但是当我执行连续的 post 方法时,我收到了 401 未经授权的错误消息。有人能解释一下我在这里犯了什么错误吗?请详细回复,因为我是 angular 和 cloudant (RESTful arch)
的新手var app = angular.module('myApp', []);
const ACCOUNTNAME = 'my acc';
const APIKEY = 'my key';
const APIPASSWORD = 'my pass';
const DATABASE = 'my db';
var cloudantDoc = 'link of document to update';
app.controller('tutorialCtrl', function($scope, $http) {
//Authentication header settings
var req = {
method: 'POST',
url: 'https://'+ACCOUNTNAME+'.cloudant.com/_session',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: 'name='+APIKEY+'&password='+APIPASSWORD,
withCredentials: true
}
$http(req).then(function(result){
//Grab data
req = {
method: 'GET',
url: 'https://'+ACCOUNTNAME+'.cloudant.com/'+DATABASE+'/_design/AllClients/_view/AllClients',
withCredentials: true
}
$http(req).then(function(result){
$scope.tableData = result.data;
console.log(result);
},
function(){
console.log("Failed at grabbing data");
});
},
function(){
console.log('Failed at authenticating');
}
);
$scope.revisionNumNews;
$http.post(cloudantDoc, JSON.stringify({"_id": "CLient1", "Client_Name":"ABC", "_rev": ""})).success(function(response){
revisionNumNews = response.rev;
});
在您的代码中有 var cloudantDoc = 'link of document to update'
,如果您尝试更新现有文档,则需要发出 PUT 请求而不是 POST。在任何情况下,您的请求都应包括 {withCredentials: true}
选项。
如果您正在尝试创建新文档,请尝试将您的 POST 请求更改为如下内容:
var databaseUrl = 'https://' + ACCOUNTNAME + '.cloudant.com/' + DATABASE
$http.post(databaseUrl, {'_id':'CLient1', 'Client_Name':'ABC'}, {withCredentials: true})
.then(function(response) {
$scope.revisionNumNews = response.rev
})
但是,如果您尝试更新现有文档,则需要发出 PUT 请求并包含文档的最新 _rev
。它看起来像这样:
var databaseUrl = 'https://' + ACCOUNTNAME + '.cloudant.com/' + DATABASE
$http.put(databaseUrl + '/' + cloudantDocId, cloudantDoc, {withCredentials: true})
.then(function(response) {
$scope.revisionNumNews = response.rev
})
其中 cloudantDocId
是正在更新的文档的文档 ID,cloudantDoc
是更新后的文档对象(最新的 _rev
)