简体   繁体   English

简单的javascript在Safari中不起作用

[英]Simple javascript doesn't work in safari

I have almost no experience in javascript and I'm trying to make my website load a bit of html code from a google drive file. 我几乎没有使用javascript的经验,并且正在尝试使我的网站从Google驱动器文件中加载一些html代码。 This works fine in every browser except for in safari. 除野生动物园外,此功能在所有浏览器中均可正常运行。 Does anyone know how to solve this issue? 有谁知道如何解决这个问题?

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(function() {
$("#leaderboard").load("https://www.googledrive.com/host/0B7VK-PTvOFWwRnVzUUlRX1BQZUU"); 
}); 
</script> 

This Security issue is know as Same Origin Policy 此安全问题被称为同一来源策略

In computing, the same-origin policy is an important concept in the web application security model. 在计算中,同源策略是Web应用程序安全模型中的重要概念。 The policy permits scripts running on pages originating from the same site – a combination of scheme, hostname, and port number 1 – to access each other's DOM with no specific restrictions, but prevents access to DOM on different sites. 该策略允许在源自同一站点的页面上运行的脚本(方案,主机名和端口号1的组合 )可以访问彼此的DOM,而没有特别的限制,但是可以阻止访问不同站点上的DOM。 1 The same-origin policy also applies to XMLHttpRequests unless the server provides an Access-Control-Allow-Origin (CORS) header. 1除非服务器提供Access-Control-Allow-Origin(CORS)标头,否则同源策略也适用于XMLHttpRequests。 Notably, WebSockets are not subject to the same-origin policy. 值得注意的是,WebSocket不受制于同源策略。

This mechanism bears a particular significance for modern web applications that extensively depend on HTTP cookies to maintain authenticated user sessions, as servers act based on the HTTP cookie information to reveal sensitive information or take state-changing actions. 这种机制对现代Web应用程序具有特殊意义,因为Web服务器广泛依赖HTTP cookie来维持经过身份验证的用户会话,因为服务器根据HTTP cookie信息进行操作以显示敏感信息或执行状态更改操作。 A strict separation between content provided by unrelated sites must be maintained on the client side to prevent the loss of data confidentiality or integrity. 必须在客户端维护不相关站点提供的内容之间的严格分隔,以防止丢失数据机密性或完整性。

jQuery.load function can't get content from another domains in some browsers due to this security issue. 由于此安全问题,jQuery.load函数无法从某些浏览器的其他域获取内容。 and the same thing is applied to XMLHttpRequests function. 同样的事情也适用于XMLHttpRequests函数。

$('#leaderboard').load('http://google.com'); //May work and may not work!

But you can use Jquery.ajax or Jquery.get function instead. 但是您可以改用Jquery.ajax或Jquery.get函数。

$.ajax({
    url: 'http://google.com',
    type: 'GET',
    success: function(res) {
        // your code
    }
});
// Works with $.get too!

Try 尝试

$(function () {
    $.ajax({
        url: window.location.protocol 
             + "//www.googledrive.com/host/0B7VK-PTvOFWwRnVzUUlRX1BQZUU",
        cache: false,
        context: $("#leaderboard"),
        dataType: "html"
    })
    .then(function(data) {
      $(this).html(data)
    }, function(jqxhr, textStatus, errorThrown) {
      console.log(errorThrown)
    });
});

jsfiddle http://jsfiddle.net/qvc27vmq/ jsfiddle http://jsfiddle.net/qvc27vmq/

See also http://browsershots.org/ 另请参阅http://browsershots.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM