这是一个非常相似的问题 AJAX, Subdomains and the 200 OK response (和) JavaScript Same Origin Policy - How does it apply to different subdomains? 但是有点扭曲。我的情况是:
与上述问题相比,我要求的是图像。
这个好像很管用 . 因为它工作得很好,当有人指出它在Firebug中产生错误时,我并没有立即想到相同的源策略。
然而,由于我所联系的那个问题,现在我想到了它,我担心它在生产中不能可靠地工作。
答:不——只是 看 就像它在工作一样;它不是真的
答:继续设置图像的SRC&wait to show,直到触发加载事件。
回答:和上面一样——去掉实际执行获取图像文件请求的步骤。
初始代码
function(imageUrl, placeTarget){ var i = new Image(); var img = $(i); img.hide() .load(imageUrl, function(e){ // console.log("loadImage: loaded"); placeTarget.attr("src", imageUrl); return true; }) .error(function(){ // error handling - do this part // console.log("loadImage: error"); return false; }); return; } // loadImage
为什么不通过创建图像元素和设置SRC将图像插入页面呢?什么更简单?
编辑:…通过JavaScript
我不确定这是否正确,但在jquery中:
img = $('<img>'); img.attr('src', 'http://somewhere.com/some_image.jpg'); $('#place_to_add').append(img); img.ready(fade_into_next);
检查此日志: JavaScript Same Origin Policy - How does it apply to different subdomains?