为什么这段代码执行失败'fetch'?
Why does this code fail to execute 'fetch'?
我正在使用服务工作者处理推送通知。我正在使用 XHR(Ajax) 方法来获取我的通知,这里是 service-worker.js:
的代码片段
var API_ENDPOINT = new Request('/getNotification', {
redirect: 'follow'});
event.waitUntil(
fetch(API_ENDPOINT, {credentials: 'include' })
.then(function(response) {
console.log(response);
if (response.status && response.status != 200) {
// Throw an error so the promise is rejected and catch() is executed
throw new Error('Invalid status code from API: ' +
response.status);
}
// Examine the text in the response
return response.json();
})
.then(function(data) {
console.log('API data: ', data);
var title = 'TEST';
var message = data['notifications'][0].text;
var icon = data['notifications'][0].img;
// Add this to the data of the notification
var urlToOpen = data['notifications'][0].link;
var notificationFilter = {
tag: 'Test'
};
var notificationData = {
url: urlToOpen,
parsId:data['notifications'][0].parse_id
};
if (!self.registration.getNotifications) {
return showNotification(title, message, icon, notificationData);
}
})
.catch(function(err) {
console.error('A Problem occured with handling the push msg', err);
var title = 'An error occured';
var message = 'We were unable to get the information for this ' +
'push message';
return showNotification(title, message);
})
);
这段代码在我第一次 运行 curl 时工作正常,但第二次我在控制台中遇到错误:
Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': Cannot construct a Request with a Request object that has already been used
这是什么意思?
不要多次重复使用 Request 对象,而是:
fetch('/getNotification', {
credentials: 'include',
redirect: 'follow'
})
问题是 API_ENDPOINT
已经被 fetch()
消耗掉了。每次将请求对象传递给 fetch 时都需要一个新的请求对象,因此 clone it 在使用它之前:
fetch(API_ENDPOINT.clone(), { credentials: 'include' })...
我正在使用服务工作者处理推送通知。我正在使用 XHR(Ajax) 方法来获取我的通知,这里是 service-worker.js:
的代码片段 var API_ENDPOINT = new Request('/getNotification', {
redirect: 'follow'});
event.waitUntil(
fetch(API_ENDPOINT, {credentials: 'include' })
.then(function(response) {
console.log(response);
if (response.status && response.status != 200) {
// Throw an error so the promise is rejected and catch() is executed
throw new Error('Invalid status code from API: ' +
response.status);
}
// Examine the text in the response
return response.json();
})
.then(function(data) {
console.log('API data: ', data);
var title = 'TEST';
var message = data['notifications'][0].text;
var icon = data['notifications'][0].img;
// Add this to the data of the notification
var urlToOpen = data['notifications'][0].link;
var notificationFilter = {
tag: 'Test'
};
var notificationData = {
url: urlToOpen,
parsId:data['notifications'][0].parse_id
};
if (!self.registration.getNotifications) {
return showNotification(title, message, icon, notificationData);
}
})
.catch(function(err) {
console.error('A Problem occured with handling the push msg', err);
var title = 'An error occured';
var message = 'We were unable to get the information for this ' +
'push message';
return showNotification(title, message);
})
);
这段代码在我第一次 运行 curl 时工作正常,但第二次我在控制台中遇到错误:
Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': Cannot construct a Request with a Request object that has already been used
这是什么意思?
不要多次重复使用 Request 对象,而是:
fetch('/getNotification', {
credentials: 'include',
redirect: 'follow'
})
问题是 API_ENDPOINT
已经被 fetch()
消耗掉了。每次将请求对象传递给 fetch 时都需要一个新的请求对象,因此 clone it 在使用它之前:
fetch(API_ENDPOINT.clone(), { credentials: 'include' })...