我正在尝试BaaSbox,一个免费的后端即服务。但它没有开箱即用的Javascript支持,我可以立即使用(目前,只有iOS和安卓)
我已经尝试按照他们的网站here上的Java说明进行操作。我只是做了一个简单的登录工作,但我总是从服务器得到错误的响应。
或者从另一方面来说,有谁知道BaaSbox的一个好的、免费的替代品?我只想能够安装在我自己的服务器上,没有付费计划或其他什么。
发布于 2014-03-29 20:27:54
在下载页面中有一个JS SDK的初步版本(几天前添加的)。文档已经准备好了,但是在zip文件中可以找到一个简单的示例。
例如,要执行注册:
//set the BaasBox parameters: these operations initialize the SDK
BaasBox.setEndPoint("http://localhost:9000"); //this is the address of your BaasBox instance
BaasBox.appcode = "1234567890"; //this is your instance AppCode
//register a new user
BaasBox.createUser("user", "pass", function (res, error) {
if (res) console.log("res is ", res);
else console.log("err is ", error);
});现在,您可以登录到BaasBox
//perform a login
$("#login").click(function() {
BaasBox.login("user", "pass", function (res, error) {
if (res) {
console.log("res is ", res);
//login ok, do something here.....
} else {
console.log("err is ", error);
//login ko, do something else here....
}
});一旦用户登录,他就可以加载属于集合的文档( SDK自动为您管理会话令牌):
BaasBox.loadCollection("catalogue", function (res, error) { //catalogue is the name of the Collection
if (res) {
$.each (res, function (i, item) {
console.log("item " + item.id); //.id is a field of the Document
});
} else {
console.log("error: " + error);
}
});然而,在幕后,SDK使用JQuery。因此,您可以查看它,以了解如何使用$.ajax调用BaasBox。
例如,creatUser()方法(注册)是:
createUser: function (user, pass, cb) {
var url = BaasBox.endPoint + '/user'
var req = $.ajax({
url: url,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({
username: user,
password: pass
}),
success: function (res) {
var roles = [];
$(res.data.user.roles).each(function(idx,r){
roles.push(r.name);
})
setCurrentUser({"username" : res.data.user.name,
"token" : res.data['X-BB-SESSION'],
"roles": roles});
var u = getCurrentUser()
cb(u,null);
},
error: function (e) {
cb(null,JSON.parse(e.responseText))
}
});
} https://stackoverflow.com/questions/22426225
复制相似问题