0

I am using javascript to render React Component.

Javascript snippet

function loadRemoteComponent(url) {
    return fetch(url)
        .then(res => res.text())
        .then(source => {
            var exports = {}
            function require(name) {               
                if (name == 'react') return React
                else throw `You can't use modules other than "react" in remote component.`
            }
            const transformedSource = Babel.transform(source, {
                presets: ['react', 'es2015', 'stage-2']
            }).code
            eval(transformedSource)
            return exports.__esModule ? exports.default : exports
        })
}

using URL, I am rendering React Component. React component is rendered using Javascript with in current solution / web site.

When I tried to render the Javascript in different web site, it shows me CORS error.

I have tried "Access-Control-Allow-Origin"; but that is not working,

It shows me error in console "Access to fetch at 'http://xxx/Scripts/components/xxx.jsx' from origin 'http://yyy' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

Do any one have any solution for this?

1 Answer 1

1

Have you tried setting the cors headers to wildcard on the server side?

// nodejs/express
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply, I am using ASP.NET core application. can you tell me where should I have to write this code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.