简体   繁体   English

(CORS)-跨域资源共享连接问题

[英](CORS) - Cross-Origin Resource Sharing connection issue

I am currently in the process of creating a browser extension for a university project. 我目前正在为大学项目创建浏览器扩展。 However as I was writing down the extension I hit a really weird problem. 但是,当我写下扩展名时,遇到了一个非常奇怪的问题。 To understand fully my situation I will need to describe it in debt from where my issue comes. 为了充分了解我的处境,我将需要在问题出处所在的债务中对其进行描述。

The extension that I am currently working on has to have a feature that checks if the browser can connect to the internet or not. 我当前正在使用的扩展程序必须具有一项功能,该功能可以检查浏览器是否可以连接到Internet。 That is why I decided to create a very simple AJAX request function and depending on the result returned by this function to determine if the user has internet connection or not. 这就是为什么我决定创建一个非常简单的AJAX请求函数,并根据此函数返回的结果来确定用户是否具有Internet连接的原因。

That is why I created this very simple AJAX function that you can see bellow this line. 这就是为什么我创建了这个非常简单的AJAX函数的原因,您可以在此行下面看到它。

 $.ajax({ url: "https://enable-cors.org/index.html", crossDomain: true, }).done(function() { console.log("The link is active"); }).fail(function() { console.log("Please try again later."); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

So far, as long as I understand what it is doing, it is working fine. 到目前为止,只要我了解它在做什么,它就可以正常工作。 For example, if you run the function as it is, it will succsesfully connect to the url and process with the ".done(function..." if you change the url to "index273.index" a file which does not exist it will process with the ".fail(function...". I was happy with the result until I decided to test it further more and unpluged my cable out of my computer. Then when I launched the extension it returned the last result from when the browser had connection with the internet. My explanation why the function is doing this is because it is caching the url result and if it cannot connect it gives the last cached value. My next step to try and solve this was to add "cache: false" after the "crossDomain: true" property but after that when I launch the extension it gives the following error: 例如,如果按原样运行该函数,则它将成功连接到该URL,并使用“ .done(function ...”)进行处理(如果将URL更改为“ index273.index”,则该文件不存在)会以“ .fail(function ...”)处理。我对结果感到满意,直到决定进一步测试并从计算机上拔下电缆。然后,当我启动扩展程序时,它返回了从浏览器已与Internet连接。我之所以要执行此功能,是因为它正在缓存url结果,如果无法连接,它将给出最后的缓存值。我尝试解决此问题的下一步是添加“ cache: “ crossDomain:true”属性后面的“ false”,但之后,当我启动扩展程序时,它会出现以下错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://enable-cors.org/index?_=1538599523573 . 跨域请求被阻止:“同源起源”策略不允许读取位于https://enable-cors.org/index?_=1538599523573的远程资源。 (Reason: CORS header 'Access-Control-Allow-Origin' missing). (原因:CORS标头“ Access-Control-Allow-Origin”缺失)。

If someone can help me out sorting this problem I would be extremely grateful. 如果有人可以帮助我解决这个问题,我将不胜感激。 I would want to apologise in advance for my English but this is not my native language. 我想提前为我的英语道歉,但这不是我的母语。

PS: I am trying to implement this function in the popup menu, not into the "content_scripts" category. PS:我正在尝试在弹出菜单中实现此功能,而不是在“ content_scripts”类别中实现。 I am currently testing this under Firefox v62.0.3 (the latest available version when I write this post). 我目前正在Firefox v62.0.3(撰写本文时的最新可用版本)下对其进行测试。

Best regards, George 最好的问候,乔治

也许不用调用URL来检查Internet连接是否可用,而可以尝试使用Navigator对象: https : //developer.mozilla.org/en-US/docs/Web/API/Navigator/connection

unless the remote server allowed origin (allowed cors) then you can't access it because it's a security issue. 除非远程服务器允许源(允许的cors),否则您将无法访问它,因为这是一个安全问题。

But there are other things you can do: 但是您还可以执行其他操作:

  1. You can load image and fire event when an image is loaded 您可以在加载图像时加载图像和触发事件
  2. You can access remote JSON via JSONP response 您可以通过JSONP响应访问远程JSON

but you can't access other pages because (unless that server allows it) it's a security issue. 但是您无法访问其他页面,因为(除非该服务器允许)这是一个安全问题。

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

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