Unable to import JavaScript Module

Asked 2 years ago, Updated 2 years ago, 88 views

I want to import my own JavaScript module (ES Module) in another file, but it doesn't work.
Failed to load the module source "file:///C:/Users/username/Programming/jsworks/ascii85/ascii85.mjs".
The error occurs:

This HTML and js code is in the same directory.

Operating System: Windows 10 1803 Browser:Firefox

//ascii85.mjs;

function ascii85encode(arr){
    letstr=";
    let a = [ ];
    for(leti=0;i<arr.length;i++){
        a. push(arr[i]);
        if(a.length===4){
            letres=ascii85_str(a);
            str+=res;
            a = [ ];
        }
    }
    if(a.length>0){
        for (let n = 0; n <4-a.length; n++) {
            a. push(0);
        }
        str+=ascii85_str(a);
    }
    return str
};

function ascii85_str(nums){
        constuint32 = createUInt32(nums);
        const str = base85_num(uint32);
        if(str==="!!!!!") {
            str = "z";
        }
        return str;
};

function createUInt32(a){
    return(a[0]<<24|a[1]<16|a[2]<8|a[3]);
};

function base85_num(n){
    n = Math.abs(n);
    letres=[];
    for(letp=0;p<5;p++){
        res.unshift(String.fromCodePoint((n%85)+33));
        n = parseInt(n/85);
    }
    return res;
};


export {ascii85encode};
<!DOCTYPE html>
<html lang="ja">
    <head>
        <metacharset="utf-8">
        <title>Test ascii 85</title>
        <script type="module">
            import {ascii85encode} from './ascii85.mjs';
            const textencoder=new TextEncoder();
            const data_uint8 = textencoder.encode("Mans");
            const result=ascii85encode(data_uint8);
            console.log(result);
        </script>
    </head>
    <body>
    </body>
</html>

javascript html firefox

2022-09-30 21:32

1 Answers

When I tried chrome, I got the following error:

Access to Script at'file://...omitted...'from origin'null' has been blocked by
CORS policy:Invalid response.Origin'null' is there before not allowed
access.

The message states that the domain name is null, so CORS Policy considers it unauthorized and is blocked.I think it's probably blocked because I can't determine which domain is Origin because I open the local HTML file directly.

Based on this error, I found the following article:

Preventing CORS Errors in a Local Environment

Here are some countermeasures:
It may be helpful.


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.