【问题标题】:How to load image from external URL in jQuery, WordPress theme?如何在 jQuery、WordPress 主题中从外部 URL 加载图像?
【发布时间】:2014-03-22 16:41:48
【问题描述】:

我已经下载了 WordPress 主题,我想根据自己的需要对其进行修改。所以我的问题是:主题中有幻灯片,它正在加载一个接一个显示的图像。我在下面发布了代码,但我们感兴趣的部分是我们将位置分配给图像的部分。假设我只有一个图像:如果我的服务器上某处有该图像,如果我在服务器上为该图像添加位置的路径http://myServer/path/to/the/image.jpg,我可以将其加载到幻灯片中。我试过了,它工作正常。问题是我要加载的图像在另一个外部 URL 上,所以当我用 http://myOtherServer/path/to/the/image.jpg 替换该行时,图像没有显示。

我在控制台得到的错误是:

Uncaught Error: Syntax error, unrecognized expression: http://myOtherServer/path/to/the/image.jpg

这是我的 .php 脚本中的 jQuery 代码:

$j(document).ready(function(){     

    $j('#kenburns_overlay').css('width', $j(window).width() + 'px');
    $j('#kenburns_overlay').css('height', $j(window).height() + 'px');
    $j('#kenburns').attr('width', $j(window).width());
    $j('#kenburns').attr('height', $j(window).height());

    $j(window).resize(function() {
        $j('#kenburns').remove();
        $j('#kenburns_overlay').remove();

        $j('body').append('<canvas id="kenburns"></canvas>');
        $j('body').append('<div id="kenburns_overlay"></div>');

        $j('#kenburns_overlay').css('width', $j(window).width() + 'px');
        $j('#kenburns_overlay').css('height', $j(window).height() + 'px');
        $j('#kenburns').attr('width', $j(window).width());
        $j('#kenburns').attr('height', $j(window).height());
        $j('#kenburns').kenburns({
            images: "http://myServer/path/to/the/image.jpg", 
            frames_per_second: 30,
            display_time: 5000,
            fade_time: 1000,
            zoom: 1.2,
            background_color:'#000000'
        });
    });
    $j('#kenburns').kenburns({
        images: "http://myServer/path/to/the/image.jpg",  
        frames_per_second: 30,
        display_time: 5000,
        fade_time: 1000,
        zoom: 1.2,
        background_color:'#000000'
    });             
});

我也尝试添加:

var imgS = new Image();
imgS.src = 'http://myOtherServer/path/to/the/image.jpg';

然后将路径替换为:

images: imgS, 

它给了我错误:

Resource interpreted as Image but transferred with MIME type text/html: "<currentUrl>/object%20HTMLImageElement/".

顺便说一句。在我的 .php 脚本的顶部,我有:

header("content-type: application/x-javascript");

所以一般的问题是:如何在我的 jQuery 代码中获取存储在另一台服务器上的图像?

编辑 1:

这是kenburns 中可能请求图像的部分:

$.fn.kenburns = function(options) {
...

        var image_paths = options.images;
        var images = [];
        $(image_paths).each(function(i, image_path){
            images.push({path:image_path,
                         initialized:false,
                         loaded:false});
        });
...
}

编辑 2:

我还尝试创建 &lt;img src="http://myOtherServer/path/to/the/image.jpg"/&gt; 标记并将其作为对象传递,但我再次收到错误:

 Resource interpreted as Image but transferred with MIME type text/html: "<currentUrl>/object%20HTMLImageElement/".

【问题讨论】:

  • 您能否具体指出导致该错误的代码行?我在您的主要代码块中的任何地方都没有看到“myOtherServer”。
  • 还有一个更重要的问题是$j.fn.kenburns 运行什么代码?
  • 发布的代码正在运行。问题是当我将http://myServer/path/to/the/image.jpg 替换为http://myOtherServer/path/to/the/image.jpg 时,它不起作用。所以它适用于来自当前服务器的图像,但不适用于来自外部 URL 的图像。
  • 我没有发布所有代码,因为它是 WordPress 主题的一部分,而且非常庞大。但基本问题是从其他 URL 加载图像。我确定未发布的代码不会导致此问题,因为来自我的服务器的 URL 运行良好:)
  • 好的,让我澄清一下我的两个问题:1)上面哪一行代码导致错误?是$j('#kenburns').kenburns()吗? 2) kenburns 内部的什么函数请求图像?听起来该函数内不允许跨域请求。

标签: jquery wordpress-theming remote-server loadimage external-url


【解决方案1】:

试试这个

html

<div id="otherImages" style="display:none !important;">
<img src="http://myOtherServer/path/to/the/image.jpg" />
</div>

编辑

js(之前没试过$.fn.kenburns

$j('#kenburns').kenburns({
        images: "http://webpage.domain" + "#otherImages",  
        frames_per_second: 30,
        display_time: 5000,
        fade_time: 1000,
        zoom: 1.2,
        background_color:'#000000'
    });

【讨论】:

  • 它不工作。它给了我:Uncaught Error: Syntax error, unrecognized expression: http://myServer#otherImages。当我只尝试"#otherImages" 时,它给了我Resource interpreted as Image but transferred with MIME type text/html:.... 之前的错误@
  • @protector 整个slideshow js 可以发布吗?是否在服务器位置检查了 file typeimages
  • There 是。它太大了,无法发布,这就是我附上文件的原因。你是什​​么意思:在服务器位置检查imagesfile types
  • @protector 和github.com/toymakerlabs/kenburns/blob/master/js/kenburns.js一样吗?不确定问题。可以尝试将文件保存到工作 url/服务器吗?
  • 对不起,我没听懂你的意思。你提议我应该将来自 github 的文件保存在我的服务器上?
猜你喜欢
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 2021-03-15
  • 2021-04-28
  • 1970-01-01
相关资源
最近更新 更多