已安装应用程序的jQuery AJAX跨源检查
我想检查用户是否安装了我的应用程序,该应用程序是使用“myapp://”协议创建的 点击一个按钮,我想检查用户是否安装了应用程序,如果安装了,将浏览器位置设置为将打开的应用程序URL方案,或者至少提示打开应用程序,否则,将用户引导到另一个页面 这就是我到目前为止所做的:-已安装应用程序的jQuery AJAX跨源检查,jquery,ajax,Jquery,Ajax,我想检查用户是否安装了我的应用程序,该应用程序是使用“myapp://”协议创建的 点击一个按钮,我想检查用户是否安装了应用程序,如果安装了,将浏览器位置设置为将打开的应用程序URL方案,或者至少提示打开应用程序,否则,将用户引导到另一个页面 这就是我到目前为止所做的:- <div> <p>Do you have my app installed?</p> <button type="button" id="appinstalled"&g
<div>
<p>Do you have my app installed?</p>
<button type="button" id="appinstalled">Yes</button>
</div>
不幸的是,由于CORS,此操作失败:-
无法加载XMLHttpRequestmyapp://example.com/index.php. 仅HTTP支持跨源请求
所以这显然是行不通的
有没有其他方法可以使用?我的网站/应用程序将主要在没有互联网接入的环境中使用,因此我无法将用户引导到相关的应用程序商店。我遇到了类似(不确切)的问题。这对我很有用:
var url = "myapp://currentpage";
var method = "GET";
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function () {
//Whoo Hoo it worked
};
xhr.onerror = function (data) {
//A million curses it did not
};
xhr.send();
注意:无论我做了什么,它都不能直接使用ajax。我需要直接使用XMLHttpRequest
另外:您需要在当前页面所在的位置正确设置目录。您需要为apache服务器更改该目录中的.htaccess
文件,以允许访问所有源。这里有大量的文档记录,所以我不会再讨论了。但是,如果您在windows服务器上,则必须更改目录中的web.config
文件
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
如果错误明确为“仅HTTP支持跨源请求”,这将如何工作?