簡體   English   中英

Baasbox和Javascript

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM