除了从此ajax调用中提取的图像之外,我的网站还具有完整的SSL

    $.ajax({
        url : 'index.php?route=taobao/taobao/related_products&product_id=<?php echo $product_id;?>&cid=<?php echo $cid;?>&tm=<?php echo $tm;?>',
        dataType : 'html',
        beforeSend : function(){
            $('#related_products').append('<div class="loading_related" style="margin:15px auto;text-align:center"><img src="public/image/load5.gif" /></div>');
        },
        complete : function(){
            $('.loading_related').remove();

            $('#related_products img').each(function() {
                var src = this.src.replace(/(http\:\/\/img\d\.tbcdn\.cn)/, "https://img.alicdn.com");
                $(this).attr('src', src);
            });
        },
        success : function(html){
            $('#related_products').html(html);
        }
    });


我无法访问模板文件来更改网址,因此我通过以下方式更改了网址

$('#related_products img').each(function() {
    var src = this.src.replace(/(http\:\/\/img\d\.tbcdn\.cn)/, "https://img.alicdn.com");
    $(this).attr('src', src);
});


这可以很好地工作,并且整个站点都是ssl,但是我遇到了混合内容错误(我猜是因为出现了不安全的请求吗?)我是否可以将我的站点完全设为ssl而不会出现mixed-content错误?

最佳答案

在将这些图像附加到DOM之前,请尝试为其设置URI协议。这是一个例子:

$.ajax({
  url: 'index.php?route=taobao/taobao/related_products&product_id=<?php echo $product_id;?>&cid=<?php echo $cid;?>&tm=<?php echo $tm;?>',
  dataType: 'html',
  beforeSend: function() {
    $('#related_products').append('<div class="loading_related" style="margin:15px auto;text-align:center"><img src="public/image/load5.gif" /></div>');
  },
  success: function(html) {
    var newHTML = $('<div/>', {
      html: html
    }).find('img').prop('src', function() {
      return this.src.replace(/(http\:\/\/img\d\.tbcdn\.cn)/, "https://img.alicdn.com");
    }).end().html();

    $('#related_products').html(newHTML);
  }
});


顺便说一句,如果一次只有一个元素.loading_related,则完整的回调在这里将变得无用。

10-07 19:52
查看更多