• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript CORS/Express: Getting data from server from domain html

GREFF1978

New Coder
Howdy!

This has been many days of trying to research however, either I am misunderstanding something or the solution online is not directred toward my issue.

I am running a node server on a sub-domain as such: xxx.yyy.com

I have express, socket.io and cors all installed.

My app.js file:

JavaScript:
const http = require('http');
const express = require('express');
const app = express();
const cors = require('cors');
const server = require('http').createServer(app);

const path = require('path');

const io = require("socket.io")(server);

app.use(cors());

app.use('/', express.static(path.join(__dirname, 'public')));

io.on("connection", (socket) => {
    
});

server.listen();

In my public folder I have a file 'testCors.html' which just outputs, Hello World.

Outside of the server, I have a file located here: www.yyy.com/testCors.php

HTML:
<script src="_js/jquery-3.6.0.min.js"></script>

<script>
$( document ).ready(function() {

            $.ajax({
                type: "GET",
                url: 'https://xxx.yyy.com/testCors.html',
                dataType: "html",
                success: function(result) {
                    alert(result);
                }
            });

        });
</script>

As you can see, I just want the yyy.com/testCors.php page to alert 'Hello World' as found from xxx.yyy.com/textCors.html

However, I continue to get the CORS error.

Any related issues I can find online on stackoverflow, YT and here and other forums seems to talk about one port on localhost to another port, or receiving API from external domain, however I want my php page on public_html to receive.

I have also tried setting headers with PHP on client side but not having any luck with that either.

Any guidance will be greatly appreciated!
 
Top