Cross-Origin Resource Sharing (CORS) using JSONP and Web Workers

白昼怎懂夜的黑 提交于 2019-11-28 10:42:42

问题


I am looking for solution how to get/send the data from/to another domain using JSONP in the Web Workers.

Since the Web Workers have not access to the DOM it is not possible to append the <script> tag with the url and callback parameter to the <head> tag from Web Workers.

Does anybody know, how to get/post the data from/to another domain using JSONP and Web Workers?

Thanks,


回答1:


CORS is a specification which has nothing to do with JSONP beyond making it obsolete in newer browsers. It enables cross-domain requests using ordinary XMLHttpRequest calls.

Here's an overview of how it works and how to use it. It can be used in Firefox 3.5+, Safari 4+, Chrome 3+, Internet Explorer 8+, and anything else using one of the same engines.




回答2:


Have a look at this code:

// Helper function to make the server requests 
function MakeServerRequest() 
{
    importScripts("http://SomeServer.com?jsonp=HandleRequest");
} 

// Callback function for the JSONP result 
function HandleRequest(objJSON) 
{
    // Up to you what you do with the data received. In this case I pass 
    // it back to the UI layer so that an alert can be displayed to prove 
    // to me that the JSONP request worked. 
    postMessage("Data returned from the server...FirstName: " 
                  + objJSON.FirstName + " LastName: " + objJSON.LastName);
} 

// Trigger the server request for the JSONP data 
MakeServerRequest();


来源:https://stackoverflow.com/questions/3616440/cross-origin-resource-sharing-cors-using-jsonp-and-web-workers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!