这是我的代码:
function sendToken(token) {
$.ajax({
url: 'https://iid.googleapis.com/iid/v1/' + token + '/rel/topics/IoT',
method: 'POST',
headers: {
Authorization: 'key=' + server_key
},
contentType: 'application/json',
cache: false,
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
}
响应是{"error":"MissingAuthorization"}
我被困住了,无法理解为什么这不起作用。
我也有同样的问题,所以我使用了 fetch 并且它可以工作。
fetch('https://iid.googleapis.com/iid/v1/'+token+'/rel/topics/'+topic, {
method: 'POST',
headers: new Headers({
'Authorization': 'key='+server_key
})
}).then(response => {
if (response.status < 200 || response.status >= 400) {
throw 'Error subscribing to topic: '+response.status + ' - ' + response.text();
}
console.log('Subscribed to "'+topic+'"');
}).catch(error => {
console.error(error);
})