javascript - 如何修复 Android WebView 中的 CORS 策略错误
问题描述
在 android webview 中调用 ajax 函数时出现问题 CORS 策略错误。我使用来自服务器的文本类型向 android 发送了以下脚本。
api_ticket.php
$ad_script = "<script>
function ad_click(link, str, id){
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if(this.responseText == '1'){ window.location.href = link;
}else{
alert('Server error');
}
}
};
xmlhttp.open('GET','https://skinexam.com/clients/ad_click_count.php?update='+str+'&id='+id,true);
xmlhttp.setRequestHeader('Access-Control-Allow-origin', '*');
xmlhttp.setRequestHeader('Access-Control-Allow-Methods', 'POST,GET, PUT, OPTIONS, DELETE');
xmlhttp.setRequestHeader('Access-Control-Max-Age', '3600');
xmlhttp.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with, content-type');
xmlhttp.send();
}
</script>";
然后,我在 android 中加载 webview。
.java file in android
txtDocReply.loadDataWithBaseURL(null, ticketModel.getTreatment(), "text/html", "utf-8", null);
ticketModel.getTreatment() 具有以下文本。
webview
"<a onclick="ad_click(link, str, id)">some text</a>
<script>
function ad_click(link, str, id){
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if(this.responseText == '1'){ window.location.href = link;
}else{
alert('Server error');
}
}
};
xmlhttp.open('GET','https://skinexam.com/clients/ad_click_count.php?update='+str+'&id='+id,true);
xmlhttp.setRequestHeader('Access-Control-Allow-origin', '*');
xmlhttp.setRequestHeader('Access-Control-Allow-Methods', 'POST,GET, PUT, OPTIONS, DELETE');
xmlhttp.setRequestHeader('Access-Control-Max-Age', '3600');
xmlhttp.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with, content-type');
xmlhttp.send();
}
</script>";
当单击 webview 中的某些文本时,我在 android webview 中调用 onclick 函数。onclick 功能运行良好。但是ajax调用有错误。
error
I/chromium: [INFO:CONSOLE(0)] "Access to XMLHttpRequest at 'https://skinexam.com/clients/ad_click_count.php?update=1&id=46' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.", source: about:blank (0)
我将从 android webview 调用服务器的 ad_click_count.php。所以 webview 应该被重定向。有什么好主意吗?请帮我。谢谢
解决方案
推荐阅读
- firebase - API 密钥对本地开发环境中的 firebase、nextjs、vercel 项目无效
- php - 使用 PHP 获取名称服务器?
- android - Android Intent Chooser 在某些情况下无法打开文件
- python - TFLite 解释器:定义最佳线程数
- pygithub - 我需要一种更快的方法来使用 PyGitHub 查找存储库
- oracle - Oracle 应用程序 - 下一步属性
- tensorflow - 检测完整的姿势图像
- google-apps-script - 如何编写代码以使用复选框在 google sheet 脚本编辑器中按顺序隐藏多个列
- javascript - How do I know when the prototype chain ends?
- python - PyTorch 中的 .data.size() 和 .size() 有什么区别?