在我的页面上使用jquery显示另一个网站的内容,而不使用Rails中的iframe

时间:2021-10-29 21:11:47

I have made a webpage on my site which will be embedded on another site using jquery. I have to provide jquery code so as when we hit the url from some another site in which my webpage is embedded , my contents of are loaded on page load. I cannot use iframe . I have used .html , .load , .ajax but they always through the error

我在我的网站上做了一个网页,用jquery嵌入到另一个网站。我必须提供jquery代码,这样当我们从我的网页嵌入的另一个站点访问url时,我的内容就会加载到页面加载中。我不能使用iframe。我使用过。html,。load,。ajax,但是它们总是通过错误

XMLHttpRequest cannot load http://mypage.com/index?user_id=82. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localserver:port' is therefore not allowed access.

XMLHttpRequest不能加载http://mypage.com/index?user_id=82。在请求的资源上不存在“访问控制允许起源”的标头。因此,不允许访问源“localserver:port”。

Is there a problem with mypage settings

mypage设置有问题吗

I have checked these:

我已经检查了这些:

$('#siteloader').load('http://mypage.com/index?user_id=82');
$("#siteloader").html('<object data="http://mypage.com/index?user_id=82">');

Please suggest

请建议

2 个解决方案

#1


2  

the error message is not actually an error, it's by design so no one can load others content without proper permission.

错误消息实际上不是错误,而是设计的,因此没有适当的权限,任何人都不能加载其他内容。

you will need to enable CORS requests for the website that you are calling.

您将需要为您正在调用的网站启用CORS请求。

for IIS7:

IIS7:

consider the following snippet (web.config)

考虑以下代码片段(web.config)

 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>

Apache server (.htaccess):

Apache服务器(. htaccess):

Header set Access-Control-Allow-Origin "*"

checkout this link for more information: http://enable-cors.org/server.html

查看此链接以获得更多信息:http://enable-cors.org/server.html

#2


2  

Thanks guys @Rory McCrossan @Mohammed ElSayed This is how I enabled CORS settings in Rails 4 Inside my application_controller

感谢大家@Rory McCrossan @Mohammed ElSayed这是我在我的application_controller中启用CORS设置的方式

after_filter :cors_set_access_control_headers

def cors_set_access_control_headers
 headers['Access-Control-Allow-Origin'] = '*'
 headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
 headers['Access-Control-Request-Method'] = '*'
 headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
end

#1


2  

the error message is not actually an error, it's by design so no one can load others content without proper permission.

错误消息实际上不是错误,而是设计的,因此没有适当的权限,任何人都不能加载其他内容。

you will need to enable CORS requests for the website that you are calling.

您将需要为您正在调用的网站启用CORS请求。

for IIS7:

IIS7:

consider the following snippet (web.config)

考虑以下代码片段(web.config)

 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>

Apache server (.htaccess):

Apache服务器(. htaccess):

Header set Access-Control-Allow-Origin "*"

checkout this link for more information: http://enable-cors.org/server.html

查看此链接以获得更多信息:http://enable-cors.org/server.html

#2


2  

Thanks guys @Rory McCrossan @Mohammed ElSayed This is how I enabled CORS settings in Rails 4 Inside my application_controller

感谢大家@Rory McCrossan @Mohammed ElSayed这是我在我的application_controller中启用CORS设置的方式

after_filter :cors_set_access_control_headers

def cors_set_access_control_headers
 headers['Access-Control-Allow-Origin'] = '*'
 headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
 headers['Access-Control-Request-Method'] = '*'
 headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
end