android Webview component cross-domain problem

(1) Use Android webView to access html pages, and when encountering ajax cross-domain access, just add it to the 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) Unable to solve the problem of cross-domain access, you can set the following configuration for 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();
            }
}

insert image description here

Guess you like

Origin blog.csdn.net/qq_36158551/article/details/129112105