如何在 Service Worker 中发出 JSONP 请求?

How to make a JSONP request inside a Service Worker?

如何在 Service Worker 中发出 JSONP 请求?

如您所知,Service Worker 没有 document。因此许多 javascript 方法如 this one 不起作用。

我需要发出 JSONP 请求,因为 Blogger API 不会使用 CORS 回答来自不同域的请求。

感谢您的回复。

Web Worker 全局有一个名为 importScripts 的方法,您可以使用它来包含脚本 urls。

所以只要定义一个回调函数,使用函数名作为callback url参数,将url传递给importScripts

webworker.js

function cb(data){
    console.log(data);
}

importScripts('http://example.com/jsonp.php?callback=cb');