1e41f4b71Sopenharmony_ci# Resolving Cross-Origin Resource Access 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Background 4e41f4b71Sopenharmony_ciFor security purposes, the ArkWeb kernel does not allow for cross-origin requests using the file or resource protocol in the URL context. As such, the **Web** component blocks such requests when loading local offline resources, and an error message similar to the following is displayed on the DevTools console: 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci``` 7e41f4b71Sopenharmony_ciAccess to script at 'xxx' from origin 'xxx' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, arkweb, data, chrome-extension, chrome, https, chrome-untrusted. 8e41f4b71Sopenharmony_ci``` 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Solutions to Local Cross-Origin Resource Access 11e41f4b71Sopenharmony_ciFor the **Web** component to load local resources across origins, use HTTP or HTTPS, instead of file or resource, as the protocol. The domain name of the URL to use should be one that you customize for individuals or organizations. Make sure it does not conflict with any existing domain name in the real world. You also need to use the [onInterceptRequest](../reference/apis-arkweb/ts-basic-components-web.md#oninterceptrequest9) API of the **Web** component to intercept and replace local resources. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciIn the following example, the **index.html** and **js/script.js** files are stored in the **rawfile** folder of the project directory. If the resource protocol is used to access **index.html**, loading **js/script.js** will fail due to cross-origin blocking. To resolve this issue, the HTTPS protocol is used instead, as in **https:\//www\.example.com/**, and the [onInterceptRequest](../reference/apis-arkweb/ts-basic-components-web.md#oninterceptrequest9) API is used to replace resources. In this way, **js/script.js** can be successfully loaded. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci```ts 17e41f4b71Sopenharmony_ci// main/ets/pages/index.ets 18e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci@Entry 21e41f4b71Sopenharmony_ci@Component 22e41f4b71Sopenharmony_cistruct Index { 23e41f4b71Sopenharmony_ci @State message: string = 'Hello World'; 24e41f4b71Sopenharmony_ci webviewController: webview.WebviewController = new webview.WebviewController(); 25e41f4b71Sopenharmony_ci // Construct a mapping table between domain names and local files. 26e41f4b71Sopenharmony_ci schemeMap = new Map([ 27e41f4b71Sopenharmony_ci ["https://www.example.com/index.html", "index.html"], 28e41f4b71Sopenharmony_ci ["https://www.example.com/js/script.js", "js/script.js"], 29e41f4b71Sopenharmony_ci ]) 30e41f4b71Sopenharmony_ci // Construct the local file and construct the return value format mimeType. 31e41f4b71Sopenharmony_ci mimeTypeMap = new Map([ 32e41f4b71Sopenharmony_ci ["index.html", 'text/html'], 33e41f4b71Sopenharmony_ci ["js/script.js", "text/javascript"] 34e41f4b71Sopenharmony_ci ]) 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci build() { 37e41f4b71Sopenharmony_ci Row() { 38e41f4b71Sopenharmony_ci Column() { 39e41f4b71Sopenharmony_ci // For the local index.html file, use HTTP or HTTPS in place of file or resource as the protocol and construct a custom domain name. 40e41f4b71Sopenharmony_ci // In this example, www.example.com is constructed. 41e41f4b71Sopenharmony_ci Web({ src: "https://www.example.com/index.html", controller: this.webviewController }) 42e41f4b71Sopenharmony_ci .javaScriptAccess(true) 43e41f4b71Sopenharmony_ci .fileAccess(true) 44e41f4b71Sopenharmony_ci .domStorageAccess(true) 45e41f4b71Sopenharmony_ci .geolocationAccess(true) 46e41f4b71Sopenharmony_ci .width("100%") 47e41f4b71Sopenharmony_ci .height("100%") 48e41f4b71Sopenharmony_ci .onInterceptRequest((event) => { 49e41f4b71Sopenharmony_ci if (!event) { 50e41f4b71Sopenharmony_ci return; 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci // Search for the local offline resource to be loaded, and then intercept and replace the resource. 53e41f4b71Sopenharmony_ci if (this.schemeMap.has(event.request.getRequestUrl())) { 54e41f4b71Sopenharmony_ci let rawfileName: string = this.schemeMap.get(event.request.getRequestUrl())!; 55e41f4b71Sopenharmony_ci let mimeType = this.mimeTypeMap.get(rawfileName); 56e41f4b71Sopenharmony_ci if (typeof mimeType === 'string') { 57e41f4b71Sopenharmony_ci let response = new WebResourceResponse(); 58e41f4b71Sopenharmony_ci // Construct the response data. If the local file is in rawfile, you can set the response data as follows: 59e41f4b71Sopenharmony_ci response.setResponseData($rawfile(rawfileName)); 60e41f4b71Sopenharmony_ci response.setResponseEncoding('utf-8'); 61e41f4b71Sopenharmony_ci response.setResponseMimeType(mimeType); 62e41f4b71Sopenharmony_ci response.setResponseCode(200); 63e41f4b71Sopenharmony_ci response.setReasonMessage('OK'); 64e41f4b71Sopenharmony_ci response.setResponseIsReady(true); 65e41f4b71Sopenharmony_ci return response; 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci return null; 69e41f4b71Sopenharmony_ci }) 70e41f4b71Sopenharmony_ci } 71e41f4b71Sopenharmony_ci .width('100%') 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci .height('100%') 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci} 76e41f4b71Sopenharmony_ci``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci```html 79e41f4b71Sopenharmony_ci<!-- main/resources/rawfile/index.html --> 80e41f4b71Sopenharmony_ci<html> 81e41f4b71Sopenharmony_ci<head> 82e41f4b71Sopenharmony_ci <meta name="viewport" content="width=device-width,initial-scale=1"> 83e41f4b71Sopenharmony_ci</head> 84e41f4b71Sopenharmony_ci<body> 85e41f4b71Sopenharmony_ci<script crossorigin src="./js/script.js"></script> 86e41f4b71Sopenharmony_ci</body> 87e41f4b71Sopenharmony_ci</html> 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci```js 91e41f4b71Sopenharmony_ci// main/resources/rawfile/js/script.js 92e41f4b71Sopenharmony_ciconst body = document.body; 93e41f4b71Sopenharmony_ciconst element = document.createElement('div'); 94e41f4b71Sopenharmony_cielement.textContent = 'success'; 95e41f4b71Sopenharmony_cibody.appendChild(element); 96e41f4b71Sopenharmony_ci``` 97