(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();

}

}

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: