How to solve 'Redirect has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header'?

前端 未结 17 2235
遥遥无期
遥遥无期 2020-11-27 13:12

I am working on an app using Vue js. According to my setting I need to pass to a variable to my URL when setting change.



        
17条回答
  •  自闭症患者
    2020-11-27 13:45

    You won't believe this, Make sure to add "." at the end of the "url"

    I got a similar error with this code:

    fetch(https://itunes.apple.com/search?term=jack+johnson)
    .then( response => {
        return response.json();
    })
    .then(data => {
        console.log(data.results);
    }).catch(error => console.log('Request failed:', error))
    

    The error I got:

     Access to fetch at 'https://itunes.apple.com/search?term=jack+johnson'
     from origin 'http://127.0.0.1:5500' 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.
    

    But I realized after a lot of research that the problem was that I did not copy the right URL address from the iTunes API documentation.

    It should have been

    https://itunes.apple.com/search?term=jack+johnson.

    not

    https://itunes.apple.com/search?term=jack+johnson

    Notice the dot at the end

    There is a huge explanation about why the dot is important quoting issues about DNS and character encoding but the truth is you probably do not care. Try adding the dot it might work for you too.

    When I added the "." everything worked like a charm.

    I hope it works for you too.

提交回复
热议问题