(1)使用Android webView访问html页面,碰到ajax跨域访问时,仅仅在header加入

http {
  ......
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers X-Requested-With;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
  ......
}

(2)无法解决跨域访问问题可以webview设置下面配置

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
           webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        }else{
            try {
                Class<?> clazz = webView.getSettings().getClass();
                Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);
                if (method != null) {
                    method.invoke(webView.getSettings(), true);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
}

在这里插入图片描述

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注