Refused to display ‘https://xxx.com.cn‘ in a frame because it set ‘X-Frame-Options‘ to ‘sameorigin‘
Android5.0+ webView+vue,使用iframe加载云端文档URL时出现了报错,加载网页失败由于响应头添加X-Frame-Option为’SAMEORIGIN’,导致了问题的产生解决办法:1、服务器修改配置2、我们可以在webView中忽略X-Frame-Option,示例代码 如下首先,在app/build.gradle中引入依赖api 'org.jsoup:jsoup:1.12
·
Android5.0+ webView+vue,使用iframe加载云端文档URL时出现了报错,加载网页失败
由于响应头添加X-Frame-Option为’SAMEORIGIN’,导致了问题的产生
解决办法:
1、服务器修改配置
2、我们可以在webView中忽略X-Frame-Option,示例代码 如下
首先,在app/build.gradle中引入依赖
api 'org.jsoup:jsoup:1.12.1'
其次,在webViewClient中配置
webView.setWebViewClient(new WebViewClient(){
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
WebResourceResponse content = getsPageContent(request.getUrl().toString());
if (content != null) {
return content;
} else {
return super.shouldInterceptRequest(view, request);
}
}
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
WebResourceResponse content = getsPageContent(url);
if (content != null) {
return content;
} else {
return super.shouldInterceptRequest(view, url);
}
}
});
private WebResourceResponse getsPageContent(String url){
if (( url.contains("http")) // guess is better to create for ex map to check all extensions
&& !(( url.contains(".js"))
||( url.contains(".css"))
||( url.contains(".tiff"))
||( url.contains(".jpg"))
||( url.contains(".png"))
||( url.contains(".gif")))){
try {
return new WebResourceResponse(
"text/html",
"UTF-8",
new ByteArrayInputStream(
Jsoup.connect(url)
.get()
.toString()
.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
更多推荐
已为社区贡献2条内容
所有评论(0)