问题
I'm learning javascript and windowsazure mobile services. As a learning lesson I created a "users" table and inserted a test user. I'm actually using icenium to write a demo app for the ipad and andoid tablets, but I can't seem to figure out even the most basic request. So I've setup a jsfiddle here: http://jsfiddle.net/MNubd/5/.
It is a simple input box:
<input id="uFullName" type="text" />
and some javascript code. I'm trying to retrieve the "name" column from the table "users" and display it in the input box:
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');
}
});
Thanks for your help!
EDIT: I had no idea the success function could only be used on server scripts. Thanks. Here is the code that ended up working for me:
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
回答1:
The options object with the success
and error
callback objects is only used in server-side scripts. For the client side, the programming model is based on promises, and you should use the done()
(or then()
) continuation to get the results:
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));
});
来源:https://stackoverflow.com/questions/19392849/azure-mobile-services-basic-read-function-in-javascript