WebGL CORS:试图突破用户代理的安全策略

WebGL CORS: An attempt was made to break through the security policy of the user agent.

本文关键字:用户代理 安全策略 突破 CORS WebGL      更新时间:2023-09-26

如何使跨来源资源共享与WebGL纹理正常工作?我想我已经采取了我需要的所有步骤。

  • 图像位于http://localhost:15555/imgbuttons/nexthand.png

  • JavaScript代码为:

    var res = new Image();
    res.crossorigin = "anonymous";
    res.onload = function () {
        if (ondone) {
            var tex = new cc.Texture2D();
            tex.initWithElement(res);
            tex.handleLoadedTexture();
            ondone(tex);
        }
        res = null;
    };
    res.src = "http://localhost:15555/imgbuttons/nexthand.png";
    

    如果.handleLoadedTexture()在以下线路上发生故障:

    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._htmlElementObj);
    

    其中,this._htmlElementObj是上述Image

  • 服务器发送带有以下响应标头的图像:

    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Methods:GET
    Access-Control-Allow-Origin:*
    

这不应该是所有必需的原料吗?我错过了什么?

我注意到Origin: null在请求头中是而不是。也许这就是问题所在,但我不确定为什么不是——res.crossorigin = "anonymous";不应该解决这个问题吗?

Doh!原因是该属性区分大小写,实际上称为crossOrigin:

res.crossOrigin = "anonymous";