[英]Baasbox and Javascript
我正在尝试免费的后端即服务BaaSbox。 但它没有立即可用的即用型Javascript支持(但仅限iOS和Android)
我无法从javascript发送正确的curl命令,有人碰巧知道一个好的资源或一个简单的$ .ajax模板吗? 我已经尝试了stackoverflow的一些示例,但是没有一个专门针对BaaSbox。
我试过以下在其网站上的Java指令在这里 。 只是使简单的登录工作有效,但我不断收到服务器的错误响应。
或者,另一方面,有人知道BaaSbox是一个很好的免费替代品吗? 我只希望能够将其安装在我自己的服务器上,无需付费计划或任何其他方式。
在下载页面中,有一个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))
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.