更新时间:2023-12-03 17:19:34
这绝对是可能的顶部从JavaScript中使用REST接口。
在您的code有两个主要问题:
使用WinJS样本是提供。我们将努力在很快有一个特定的PhoneGap样本!
I'm trying to get a registration on the Azure Notification Hub working from html/javascript code running in a web view host (Phonegap / Intel XDK). There is no client library available, so I try to use the REST API (documentation: ).
I have the following Javascript code:
function registerWithAzureNotificationHub()
{
var sas = "Endpoint=sb://eventpusher-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=69XuYoluyBKl6JkkN03Z1oNC7cFSZ4Ku0ZWmPuWoJzs=";
var data = '<?xml version="1.0" encoding="utf-8"?>\
<entry xmlns="http://www.w3.org/2005/Atom">\
<content type="application/xml">\
<MpnsRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">\
<Tags>myTag, myOtherTag</Tags>\
<ChannelUri>https://eventpusher-ns.servicebus.windows.net/eventpusher</ChannelUri>\
</MpnsRegistrationDescription>\
</content>\
</entry>';
if (AppMobi.iswp8) {
window.alert("IS WP8");
}
else
{
window.alert("IS NOT WP8");
}
$.ajax({
type:"POST",
url: "https://eventpusher-ns.servicebus.windows.net/EVENTPUSHER/registrations/?api-version=2013-08",
contentType: "application/atom+xml;type=entry;charset=utf-8",
headers: {
"Authorization": sas,
"x-ms-version": "2013-08"
},
dataType: "xml",
data: data,
success: function(d) { window.alert("SUCCESS!"); },
error: function(msg) { window.alert("FAILURE:" + JSON.stringify(msg)); }
});
window.alert("SENT!");
}
In the above case I use the Intel XDK with the code running on a WP8 device, so I register for MPNS (Microsoft Push Notification Service).
The above code fails, and returns without descriptive information about the cause of the error.
Questions:
It is definitely possible top use the REST interface from javascript. In your code there are two main problems:
A sample using WinJS is available. We will work on having a PhoneGap specific sample very soon!