javascript - 将 authorize.net 商家印章放在 augular js View 文件中

标签 javascript angularjs asynchronous authorize.net

我有带支付功能的 angular js 应用程序。我想插入 authorize.net 印章代码(由 authorize.net 为经过验证的商家提供。我将我的站点添加到 authorize.net 服务器上的已验证商家印章域列表中):

<!-- (c) 2005, 2014. Authorize.Net is a registered trademark of CyberSource Corporation --> <div       class="AuthorizeNetSeal"> <script type="text/javascript" language="javascript">var ANS_customer_id=my_customer_id;</script> <script type="text/javascript" language="javascript" src="//verify.authorize.net/anetseal/seal.js" ></script> <a href="http://www.authorize.net/" id="AuthorizeNetText" target="_blank">Merchant Services</a> </div>

当我插入我的一个 View 页面时,它显示以下错误

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

我明白,加载 View 后它无法写入印章图像(bcoz seal.js 包含 document.write 代码)。但我不知道如何解决这个问题。

如果我将此代码放在主 index.html 中,它可以正常工作(显然)。但我需要它在我的一个 View 文件中。请帮我解决这个问题。

提前致谢。

最佳答案

问题是 document.writeln() 不会从异步加载的 seal.js 脚本运行,因为页面已经加载。使 document.writeln() 和 document.write() 异步工作是不可能的,因此您必须解决它。

有两种解决方案...

第一个解决方案是下载 seal.js 脚本并将其托管在您的网站上,用字符串替换所有 document.writeln() 函数。

我在这里完成了这个(seal.js 文件在 js 选项卡中): <强> http://codepen.io/anon/pen/WbxJEd

document.write()document.writeln() 替换为

var htmlString = '';
htmlString += '<img src="blabla"/>';

第二种解决方案(如果您不想托管 js,这可能是更好的选择)是覆盖 document.write()document .writeln() 函数与你自己的....

http://codepen.io/anon/pen/JoKvyg

document.writeOld = document.write;
document.writelnOld = document.writeln;

//Getting the element we are rendering too and setting string and custom function
var placeholder = document.getElementById('sealPlaceholder'),
    sealHtml = '',
    customWriteFnc = function(html) {
        sealHtml += html;
        placeholder.innerHTML = sealHtml;
    }

//Overwriting document.write and document.writeln with our own function
document.write = customWriteFnc;
document.writeln = customWriteFnc;

//Loading the script after the page has loaded
setTimeout(function(){
    var head = document.getElementsByTagName('head')[0],
        script = document.createElement('script');
   script.type= 'text/javascript';
   script.src= '//verify.authorize.net/anetseal/seal.js';
   head.appendChild(script);
}, 500);

关于javascript - 将 authorize.net 商家印章放在 augular js View 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498433/

相关文章:

javascript - 元素无法位于 (x,y) : how to fix for a list of WebElements that need to be clicked one by one

asp.net - AJAX Web 服务 - Web 或商务层的扩展?

javascript - 为什么这个 angularjs 指令不起作用?

javascript - 如何放入将从另一台服务器加载数据的 HTML 代码?

javascript - html &lt;input&gt; 无法将值传递给 URL,除非使用 codeigniter 的 input_form

javascript - 使用 jquery 将我的内容加载到 div 后如何获得单独的链接?

javascript - Highcharts-ng 指令( Angular )不更新

javascript - AngularJS 从 Controller 更新 ng-click 范围

node.js - NodeJS 异步/等待和函数调用

c++ - 如何将结构元素传递给异步线程?