Azure 移动服务:JavaScript 中的基本读取功能



我正在学习javascript和windowsazure移动服务。 作为学习课程,我创建了一个"用户"表并插入了一个测试用户。 我实际上正在使用icenium为ipad和andoid平板电脑编写演示应用程序,但我似乎甚至无法弄清楚最基本的请求。所以我在这里设置了一个 jsfiddle:http://jsfiddle.net/MNubd/5/。

它是一个简单的输入框:

<input id="uFullName" type="text" />

和一些JavaScript代码。 我正在尝试从"用户"表中检索"名称"列并将其显示在输入框中:

alert('running');
var client = new WindowsAzure.MobileServiceClient('https://mtdemo.azure-mobile.net/', 'MtxOqGpaBzuPRtnkIifqCKjVDocRPY47');
usersTable = client.getTable('users');
usersTable.where({ userID: 'impretty@blockedheaded.com' }).read({
    success: function (results) {
        $('#uFullName').val(results);
    },
    error: function (err) {
        $('#uFullName').val('there was and err');
    }
});

感谢您的帮助!

编辑:我不知道成功功能只能在服务器脚本上使用。 谢谢。 这是最终为我工作的代码:

function signInButton(e) {
    var un = $('#username');
    uName = un.val();
    var client = new WindowsAzure.MobileServiceClient('https://mtdemo.azure-mobile.net/', 'MtxOqGpaBzuPRtnkIifqCKjVDocRPY47');    
    //alert('lookup: ' + uName);
    usersTable = client.getTable('users');    
    usersTable.where({ userID: uName })
    .read()
    .done(
        function (results) {            
            try {
                xUserName = results[0].name; //using this to trigger an exception if the login credentials don't match.
                xUserID = results[0].id; // save this for querying and adding records for this user only.
                //alert('found');
                app.application.navigate('#view-menu');                        
            }
            catch(err) {
                 //alert('not found');
                 document.getElementById('errorText').textContent = "Check Email and Password!";                 
            }            
        } 
    );//end of the done

具有 successerror 回调对象的选项对象仅用于服务器端脚本。对于客户端,编程模型基于 promises,您应该使用 done()(或 then() )延续来获取结果:

var client = new WindowsAzure.MobileServiceClient('https://mtdemo.azure-mobile.net/', 'YOUR-KEY');
var usersTable = client.getTable('users');
usersTable.where({ userID: 'impretty@blockheaded.com' }).read().done(function (result) {
    $("#uFullName").val(result.name);
}, function (err) {
    $("#uFullName").val('There was an error: ' + JSON.stringify(err));
});

最新更新