简体   繁体   English

链接外部JS库并出现XMLHttpRequest错误

[英]Linking an external JS library and getting XMLHttpRequest error

I need to use a JavaScript library and have it referenced as such: 我需要使用一个JavaScript库,并这样引用它:

    <script type="text/javascript" src="https://js.authorize.net/v1/Accept.js"></script>

However I get an error that states: 但是我得到一个错误,指出:

XMLHttpRequest cannot load https://js.authorize.net/v1/AcceptCore.js . XMLHttpRequest无法加载https://js.authorize.net/v1/AcceptCore.js No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。

I've researched this issue and tried various bits of code and ideas, but it hasn't worked for me. 我已经研究了这个问题,并尝试了各种代码和想法,但是它对我没有用。 I am using ASP.NET, webforms, and c#. 我正在使用ASP.NET,Webforms和C#。 I've tried adding various code to my web.config, such as 我尝试将各种代码添加到我的web.config中,例如

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

and

   <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Content-Type" />
            <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
          </customHeaders>
        </httpProtocol>
      </system.webServer>

I also tried the recommendation of installing Cors, but I am using framework 4.0. 我还尝试了安装Cors的建议,但我使用的是Framework 4.0。 Cors requires 5.0 and failed to install. Cors需要5.0,并且无法安装。

The script you're loading is just turning right around and using XMLHttpRequest to load another one (also from js.authorize.net); 您正在加载的脚本只是转过来使用XMLHttpRequest来加载另一个脚本(同样来自js.authorize.net); that request is being denied because your page's origin isn't allowed access. 该请求被拒绝,因为您页面的原始位置不允许访问。 Looks like some kind of authorization thing to keep their JS from being downloaded on unauthorized origins. 看起来像某种授权的事情,以防止其JS从未经授权的来源下载。

Nothing you do on your end changes the CORS rules at the js.authorize.net end. 您所做的任何事情都不会更改js.authorize.net端的CORS规则。 It's that server, js.authorize.net, that has to allow your origin to do the request. 正是服务器js.authorize.net必须允许您的来源执行请求。

You'll need to work with the people behind js.authorize.net in order to get access. 您需要与js.authorize.net背后的人员合作才能获得访问权限。

This may have been added since the original post, but for others to know, Authorize.net addresses this on the Accept.js page on their site. 这可能是自原始帖子以来添加的,但是让其他人知道,Authorize.net在其站点的Accept.js页面上解决了此问题。
Link: https://developer.authorize.net/api/reference/features/acceptjs.html 链接: https//developer.authorize.net/api/reference/features/acceptjs.html

NOTE: If you are debugging, you may see an XMLHttpRequest error in the JS console. 注意:如果要调试,则可能会在JS控制台中看到XMLHttpRequest错误。 This is because our core processing library cannot be called directly, and this is an expected security warning. 这是因为不能直接调用我们的核心处理库,这是预期的安全警告。

Everything still works despite the error. 尽管出现错误,一切仍然有效。

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

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