1# Resolving Cross-Origin Resource Access
2
3## Background
4For 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:
5
6```
7Access 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.
8```
9
10## Solutions to Local Cross-Origin Resource Access
11For 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.
12
13In 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.
14
15
16```ts
17// main/ets/pages/index.ets
18import { webview } from '@kit.ArkWeb';
19
20@Entry
21@Component
22struct Index {
23  @State message: string = 'Hello World';
24  webviewController: webview.WebviewController = new webview.WebviewController();
25  // Construct a mapping table between domain names and local files.
26  schemeMap = new Map([
27    ["https://www.example.com/index.html", "index.html"],
28    ["https://www.example.com/js/script.js", "js/script.js"],
29  ])
30  // Construct the local file and construct the return value format mimeType.
31  mimeTypeMap = new Map([
32    ["index.html", 'text/html'],
33    ["js/script.js", "text/javascript"]
34  ])
35
36  build() {
37    Row() {
38      Column() {
39        // 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.
40        // In this example, www.example.com is constructed.
41        Web({ src: "https://www.example.com/index.html", controller: this.webviewController })
42          .javaScriptAccess(true)
43          .fileAccess(true)
44          .domStorageAccess(true)
45          .geolocationAccess(true)
46          .width("100%")
47          .height("100%")
48          .onInterceptRequest((event) => {
49            if (!event) {
50              return;
51            }
52            // Search for the local offline resource to be loaded, and then intercept and replace the resource.
53            if (this.schemeMap.has(event.request.getRequestUrl())) {
54              let rawfileName: string = this.schemeMap.get(event.request.getRequestUrl())!;
55              let mimeType = this.mimeTypeMap.get(rawfileName);
56              if (typeof mimeType === 'string') {
57                let response = new WebResourceResponse();
58                // Construct the response data. If the local file is in rawfile, you can set the response data as follows:
59                response.setResponseData($rawfile(rawfileName));
60                response.setResponseEncoding('utf-8');
61                response.setResponseMimeType(mimeType);
62                response.setResponseCode(200);
63                response.setReasonMessage('OK');
64                response.setResponseIsReady(true);
65                return response;
66              }
67            }
68            return null;
69          })
70      }
71      .width('100%')
72    }
73    .height('100%')
74  }
75}
76```
77
78```html
79<!-- main/resources/rawfile/index.html -->
80<html>
81<head>
82	<meta name="viewport" content="width=device-width,initial-scale=1">
83</head>
84<body>
85<script crossorigin src="./js/script.js"></script>
86</body>
87</html>
88```
89
90```js
91// main/resources/rawfile/js/script.js
92const body = document.body;
93const element = document.createElement('div');
94element.textContent = 'success';
95body.appendChild(element);
96```
97