Cross-origin image load denied by Cross-Origin Resource Sharing policy

隐身守侯 提交于 2019-12-20 03:09:27

问题


I use html2canvas (from html2canvas.hertzen.com) to capture screenshot. I got this strange error like this: The code of my webpage is put on one host, say Host A. If my webpage contains an image on another host, say Host B, then I hit this error: Cross-origin image load denied by Cross-Origin Resource Sharing policy

However, the confusing part is that if Host B is facebook (my image is a direct link to facebook https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/372701_100000684388457_1551561655_q.jpg) then the error disappear.

My function

html2canvas([document.body], {
                    useCORS : true,
                    logging : true,
                    onrendered : function(canvas) {
                        document.body.appendChild(canvas);
                        var myImage = canvas.toDataURL("image/png");
                        window.open(myImage);
                    }

Anyone got a tip? Tks

solution

 html2canvas([document.body], {
                                    useCORS: true,
                                    proxy: "Server",
                                    onrendered : function(canvas) {                               
                                        ListUCapture = canvas.toDataURL("image/png");                                                           
                                    }
                     });
Server is server of node.js

回答1:


If you wish to load cross-origin images to a canvas, you need to either serve the image with cross-origin headers or under the same origin. That image under Facebook is served with the following header option set:

Access-Control-Allow-Origin:*

Meaning, it can be cross-origin loaded with the useCORS option. However, it would appear that your Host B isn't serving them with cross-origin headers set.



来源:https://stackoverflow.com/questions/17990678/cross-origin-image-load-denied-by-cross-origin-resource-sharing-policy

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