user5844628
user5844628

Reputation: 419

Isomorphic fetch gives CORS error

I am trying to covert my jQuery ajax call to use isomorphic fetch instead

My jQuery ajax call looks like this:

$.ajax({
    url: 'myURL',
    dataType: "script",
    cache: true,
    success() {
        notify();
    }
});

and my isomorphic fetch looks like this:

 var myHeaders = new Headers();
 myHeaders.append('Content-Type', 'application/javascript');
 fetch('myURL', {
    headers: myHeaders,
    mode: 'cors',
    cache: 'default'
 }).then(function(){
     notify();
 }).catch(function(error) {
     console.log('request failed', error)
 })

In the isomorphic-fetch version I am getting a CORS error in the browser:

"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:8080' is therefore not allowed access. The response had HTTP status code 501. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

However, the ajax call works fine.

What am I doing wrong?

Upvotes: 0

Views: 3367

Answers (0)

Related Questions