简体   繁体   中英

I get CORS error on javascript module file

在此处输入图像描述

When I add a spline 3d element in javascript I have to use module to use it and then I get that error it says ccess to script at 'file:///D:/js%20learn/spline.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

Please help me. I need to use that spline 3d element

I tried to upload that javascript file on a server to use it as an http link but I don't know how

You will need a web server to serve over the http protocol. For development you can use a localhost . The easiest way I know how to do that is to install NodeJS on your system. Then open a terminal or command line prompt (I think on windows this is run ) and install the server stuff by running this command:

npm install http-server -g

Now drag the root folder of your project into the terminal to go to that folder (or somehow get to that folder, if you are on Windows I can't exactly tell you). From that location, run this command:

http-server

It will log a bunch a things, but most importantly it will allow you to visit your project as if it was a website on a server. It's usually located in the localhost domain, like localhost:8080 . This should prevent any CORS/origin/protocol issues in general (there are some other cors issues you could run into but deal with those when you get to them).

There are also solutions that don't require you to use a terminal at all like MAMP . Point it at your folder and start a server. But if you are going to continue in web development, making yourself familiar with terminal and command line will be a boon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM