简体   繁体   English

Baasbox和Javascript

[英]Baasbox and Javascript

I'm trying BaaSbox, a free Backend as a Service. 我正在尝试免费的后端即服务BaaSbox。 But it has no out-of-the-box Javascript support I can use right away (yet, only iOS and Android) 但它没有立即可用的即用型Javascript支持(但仅限iOS和Android)

I'm having trouble sending the right curl command from javascript, anyone happen to know a good resource or a simple working $.ajax template? 我无法从javascript发送正确的curl命令,有人碰巧知道一个好的资源或一个简单的$ .ajax模板吗? I've tried a few examples from stackoverflow, but none of them specifically aimed at BaaSbox. 我已经尝试了stackoverflow的一些示例,但是没有一个专门针对BaaSbox。

I've tried following the Java instructions on their site here . 我试过以下在其网站上的Java指令在这里 Just making a simple login work, but I keep getting the wrong responses from the server. 只是使简单的登录工作有效,但我不断收到服务器的错误响应。

Or on the other hand, anyone know a good, free alternative to BaaSbox? 或者,另一方面,有人知道BaaSbox是一个很好的免费替代品吗? I just want to be able to install it on my own server, no paid plans or whatever. 我只希望能够将其安装在我自己的服务器上,无需付费计划或任何其他方式。

in the download page there is a preliminary version of the JS SDK (added few days ago). 在下载页面中,有一个JS SDK的初步版本(几天前添加)。 The documentation is on the way, however in the zip file you can find a simple example. 文档正在编写中,但是在zip文件中,您可以找到一个简单的示例。

For example to perform a signup: 例如执行注册:

//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);
});

Now you can login into BaasBox 现在您可以登录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....
                 }
    });

Once the user is logged in he can load the Documents belonging to a Collection (the SDK automatically manages the Session Token for you): 用户登录后,他可以加载属于集合的文档(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);             
        }       
});

However under the hood the SDK uses JQuery. 但是,SDK在后台使用JQuery。 So you can inspect it to know how to user $.ajax to call BaasBox. 因此,您可以检查它以了解如何使用$ .ajax调用BaasBox。

For example the creatUser() method (signup) is: 例如,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM