apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.16k stars 990 forks source link

sub html page XMLHttpRequest cannot load (json file) due to access control checks #1260

Closed Jung-Hey closed 2 years ago

Jung-Hey commented 2 years ago

스크린샷 2022-09-20 오전 9 02 16

I am getting the above error. The same error occurred in the main index.html file, but it was easily solved by downloading the cordova-plugin-wkwebviewxhrfix plugin. The problem was that through the wkwebviewxhrfix plugin, the XMLHttpRequest cannot load error in the main index.html file was solved, but the sub page index.html file did not solve the same error. I need a solution.

스크린샷 2022-09-19 오후 5 47 14

스크린샷 2022-09-19 오후 5 47 39

(main) index.html path : www/index.html (sub) index.html path : www/world/wikitude_AR/inedex.html

Version information

breautek commented 2 years ago

WKWebView enforces CORS and there is no way to disable that, but there are workarounds.

If you have not already migrated to cordova-ios version 6, then these workarounds may not work. Therefore I recommend migrating to the latest version of cordova-ios@6 or later.

If possible, you should be using schemes. To enable schemes, you can use this config in your config.xml

<preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />

Using schemes will lift your app off the file protocol, which intentionally has several restrictions on the webview that are generally unavoidable or difficult to work around. However, if you have an existing app, changing from file-based app to a scheme based app will cause an origin change, and thus you'll lose access to any previously stored web storage data such as local storage or cookies. The data will still exists, but will remain inaccessible. Data migrations is out of scope of what Cordova can provide support for.

If you must continue to use the file-protocol system (which is the cordova default), then you must use a third-party plugin that proxies XHR requests to conduct the HTTP request, rather than using the browser XMLHttpRequest feature, which is bounded by CORS. I know there are many users who have had success using Oracle's plugin that does just this.

Switching to scheme solution should be the first option to consider, if possible. Only use the file-protocol system if you cannot make the switch or migrate to schemes.

Jung-Hey commented 2 years ago

I have already tried how to modify config.xml and how to use plugins. The core of the problem is to solve the security problem of sub index.html. Please refer to the path.