首页 > 解决方案 > 打开tel:XXXXXXXXXX时如何通过JS添加CORS

问题描述

如果我们从 Android 手机访问,我需要通过 Java 脚本打开本机拨号程序。由于ionincframework不允许通过普通代码打开本机拨号程序,因此做了一些研究并了解到由于 CORS 它无法打开。在使用 tel:XXXXXXXXXXX 协议之前尝试使用以下代码应用 CORS,但无法正常工作。

<html>
<div class="phoneLinkDiv" onclick="javascript:createCORSRequest('POST','tel:555-999-8888');" data-rel="external">Hello</div>
<a href="tel:555-999-8888">Href</a> // Even this one also not working
<script>
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    alert("Inside with credentials");
    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    alert("Inside without credentials");
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    // Otherwise, CORS is not supported by the browser.
    xhr = null;
    alert("Inside without credentials CORS not supported");

  }
  return xhr;
}


</script>
</html>

建议我任何解决方法,如果我错了,请纠正我。但在 chrome 和 safari 中工作的正常代码相同。

标签: javascriptandroidionic-frameworkmobile

解决方案


添加到您的 config.xml:

<allow-intent href="tel:*" />

然后使用:

<a href="tel:555-999-8888">Href</a>

推荐阅读