首页 > 解决方案 > 如何修复 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 应该被重定向。有什么好主意吗?请帮我。谢谢

标签: javascriptjavaandroidwebview

解决方案


推荐阅读