Mobile/Android
[Android] 로컬 웹뷰 ajax cross origin 에러
oSsonGo
2020. 12. 5. 12:42
Android 로컬 웹뷰(file:///android_asset/index.html)에서 내부 파일을 ajax로 불러올 때 cross origin 이슈가 발생
ex) test.json 파일을 로드
$.ajax({
url : "test.json",
success : function (data) {
console.log(data);
}
});
하기와 같이 에러 발생
Access to XMLHttpRequest at 'file:///android_asset/test.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, https.
WebView Setting 에 하기 추가
WebSettings mWebViewSetting = mWebView.getSettings();
mWebViewSetting.setAllowFileAccessFromFileURLs(true);
mWebViewSetting.setAllowUniversalAccessFromFileURLs(true);