Azure移动服务和Javascript

时间:2016-07-14 20:21:45

标签: javascript authentication azure-mobile-services

嗨,我被卡住,不知何故找不到解决方案。看起来很简单但是,好吧。在这里。我在Azure有一个移动服务,我想用javascript到达那个。我如何绕过401 Unauthorized?我尝试使用MS提供的文档,但没有运气。这是我到目前为止所做的(当然,添加网址的关键不起作用)我可以添加什么来让它工作?

var client = new WindowsAzure.MobileServiceClient(
"https://cdshop.azure-mobile.net/",
"vGpqzyApJXXXXXXXXblQCWne73"
);

var getJSON = function (url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function () {
    var status = xhr.status;
    if (status == 200) {
        callback(null, xhr.response);
    } else {
        callback(status);
    }
};
xhr.send();
};


$(function () {
$('#clickme').click(function () {

    getJSON('http://cdshop.azure-mobile.net/api/cds/total?key=vGpqzyApJXXXXXXXXblQCWne73', function (err, data) {
if (err != null) {
    alert('Something went wrong: ' + err);
} else {
    alert('Your Json result is:  ' + data.result);
    result.innerText = data.result;
}
    });
});
});

1 个答案:

答案 0 :(得分:1)

如果您要创建自己的HTTP请求,则需要使用应用程序密钥设置名为X-ZUMO-APPLICATION的请求标头,例如: “vGpqzyApJXXXXXXXXblQCWne73”,用于设置为“application”或“users”的表和API。 (假设您仍在使用移动服务;较新的App Service不使用此X-ZUMO-APPLICATION标头。)为“用户”设置的表和API还需要一个带有用户身份验证令牌的X-ZUMO-AUTH请求标头。 / p>

或者,您可以使用在第一行中创建的MobileServiceClient,它将为您执行此操作。 This page包含调用API和表的示例。以你的例子:

client.invokeApi("cds", {
    body: null,
    method: "get"
}).done(function (data) {
    alert('Your Json result is:  ' + data.result);
    result.innerText = data.result;
}, function(error) {
    alert('Something went wrong: ' + error);
});