Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a JSONP request inside a Service Worker?

How to make a JSONP request inside a Service Worker?

As you may know, a Service Worker doesn't have document. Therefore many javascript approaches like this one don't work.

I need to make a JSONP request because the Blogger API doesn't answer requests from different domains using CORS.

Thank you for your response.

like image 933
melanke Avatar asked Mar 14 '23 18:03

melanke


1 Answers

The Web Worker global has a method called importScripts, which you can use to include script urls.

So just define a callback function, use the function name as the callback url parameter and pass the url to importScripts

webworker.js

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

importScripts('http://example.com/jsonp.php?callback=cb');
like image 134
Patrick Evans Avatar answered Mar 24 '23 19:03

Patrick Evans