首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Baasbox和Javascript

Baasbox和Javascript
EN

Stack Overflow用户
提问于 2014-03-16 00:07:46
回答 1查看 1.1K关注 0票数 1

我正在尝试BaaSbox,一个免费的后端即服务。但它没有开箱即用的Javascript支持,我可以立即使用(目前,只有iOS和安卓)

我已经尝试按照他们的网站here上的Java说明进行操作。我只是做了一个简单的登录工作,但我总是从服务器得到错误的响应。

或者从另一方面来说,有谁知道BaaSbox的一个好的、免费的替代品?我只想能够安装在我自己的服务器上,没有付费计划或其他什么。

EN

回答 1

Stack Overflow用户

发布于 2014-03-29 20:27:54

在下载页面中有一个JS SDK的初步版本(几天前添加的)。文档已经准备好了,但是在zip文件中可以找到一个简单的示例。

例如,要执行注册:

代码语言:javascript
复制
//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

代码语言:javascript
复制
//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自动为您管理会话令牌):

代码语言:javascript
复制
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()方法(注册)是:

代码语言:javascript
复制
    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))
            }
        });

    } 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22426225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档