简体   繁体   English

asp.net中出现“Access-Control-Allow-Origin”标题错误

[英]Error “Access-Control-Allow-Origin” header in asp.net

I have 2 project are running parallel. 我有2个项目正在并行运行。 http://localhost:497 and http://localhost:580 http:// localhost:497http:// localhost:580

On http://localhost:580 I want to get file html form folder PDF in http://localhost:497 into iframe http:// localhost:580我想将http:// localhost:497中的文件html表单文件夹PDF转换为iframe

<iframe src="http://localhost:497/PDF/test.html"></iframe>

After that, i was received an error: 之后,我收到了一个错误:

XMLHttpRequest cannot load http://localhost:497/PDF/test.html . XMLHttpRequest无法加载http:// localhost:497 / PDF / test.html No 'Access-Control-Allow-Origin' header is present on the requested resource. 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://localhost:580 ' is therefore not allowed access. 因此不允许Origin'http:// localhost:580 '访问。

and I can't use jquery for get effect to content in iframe . 我不能使用jquery来获取iframe中的内容。

I don't know how to fix that error, please help me. 我不知道如何解决这个错误,请帮助我。 Thanks All. 谢谢大家。

All modern browsers disallow cross domain requests for security purposes. 所有现代浏览器都出于安全目的而禁止跨域请求。 When an XMLHttpRequest is made from one domain to another (localhost:580 to localhost:497 in this case), it will send what is known as a pre-flight request to the server to see if the requesting server has access. 当XMLHttpRequest从一个域到另一个域(在这种情况下为localhost:580到localhost:497)时,它将向服务器发送所谓的飞行前请求,以查看请求服务器是否具有访问权限。 The appropriate headers must be present in order for the request to be accepted, which happens on a second trip once the initial preflight handshake is verified. 必须存在适当的标题才能接受请求,一旦验证了初始预检握手,就会在第二次行程中发生。 Here is a link to CORS documentation from Mozilla: 以下是Mozilla的CORS文档链接:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

If you enhance your web.config file in visual Studio, it should works. 如果在Visual Studio中增强web.config文件,它应该可以正常工作。 As far as I know, preflight generates automatically a OPTIONS request, so you also need to allow OPTIONS requests. 据我所知,preflight会自动生成一个OPTIONS请求,因此您还需要允许OPTIONS请求。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="http://localhost:580" />
       <add name="Access-Control-Allow-Methods" value="GET,OPTIONS" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

暂无
暂无

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

相关问题 asp.net Web服务中的Access-Control-Allow-Origin - Access-Control-Allow-Origin in asp.net web service ASP.NET Web窗体:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - ASP.NET Web Forms: No 'Access-Control-Allow-Origin' header is present on the requested resource Ajax获取对ASP.NET MVC控制器的调用被视为“ NO&#39;Access-Control-Allow-Origin&#39;错误 - Ajax Get call to ASP.NET MVC controller is treated as "NO ‘Access-Control-Allow-Origin’ error Google翻译时出现XMLHttpRequest错误(没有&#39;Access-Control-Allow-Origin&#39;标题) - XMLHttpRequest error with Google translate (No 'Access-Control-Allow-Origin' header) Zillow API错误:“不存在“ Access-Control-Allow-Origin”标头” - Zillow API Error:“No 'Access-Control-Allow-Origin' header is present” AJAX请求中没有“ Access-Control-Allow-Origin”标头存在错误 - No 'Access-Control-Allow-Origin' header is present error in AJAX Request jQuery.getJSON()上没有&#39;Access-Control-Allow-Origin&#39;标头错误 - No 'Access-Control-Allow-Origin' header Error on jQuery.getJSON() CORS错误:不存在“ Access-Control-Allow-Origin”标头 - CORS Error : No 'Access-Control-Allow-Origin' header is present No 'Access-Control-Allow-Origin' header is present on the requested resource error - No 'Access-Control-Allow-Origin' header is present on the requested resource error 控制台上没有“ Access-Control-Allow-Origin”错误 - No 'Access-Control-Allow-Origin' error on console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM