0

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?

Adil
  • 240
  • 1
  • 6
  • 19
user5844628
  • 397
  • 1
  • 5
  • 15
  • Do you need the CORS mode? Your $.ajax doesn't use CORS either… – Patrick Hund Feb 27 '17 at 07:39
  • even if i get rid of it, I still get the CORS error – user5844628 Feb 27 '17 at 07:41
  • are the server you're posting to and the one that delivers your HTML page running on different ports? If so, you need CORS indeed. Add it to the response header that the server delivers. See http://stackoverflow.com/questions/18642828/origin-http-localhost3000-is-not-allowed-by-access-control-allow-origin – Patrick Hund Feb 27 '17 at 07:44
  • Possible duplicate of [Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin](http://stackoverflow.com/questions/18642828/origin-http-localhost3000-is-not-allowed-by-access-control-allow-origin) – Patrick Hund Feb 27 '17 at 07:45
  • I tried adding 'Access-Control-Allow-Origin': '*' to the header but still get the same CORS error – user5844628 Feb 27 '17 at 08:16
  • Did you ever figure this out? I've got the same issue. I've got CORS set up fine on my server, but I get cors errors from isomorphic-fetch. – Tom Apr 19 '17 at 10:23

0 Answers0