A
A
AlexLF2021-05-25 21:47:19
JavaScript
AlexLF, 2021-05-25 21:47:19

How to download js code from another site?

I use two sites. I want to use the js file I wrote from the first site on the second site. I'm trying to load code with fetch or import(). I get a CORS error in the console. How to download js code?

If it was html I would insert the headers with php in the import file and it rolls over:

<?php
header('Content-type: text/html');
header('Access-Control-Allow-Origin: *');
?>

I try to insert something like this at the beginning of the imported file, but it doesn't help:

let myHeaders = new Headers();
myHeaders.set('Content-type', 'text/javascript');
myHeaders.set('Access-Control-Allow-Origin', '*');

I have JS code in ES modules, so I can't load it with <script src=''>.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hecktosaurus, 2021-05-26
@Hecktosaurus

Write a simple php script with file_get_contents(), make a request from the front to this script, and let the script get the file from a third-party server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question