psolbach / iconbin

Beautiful favicons API
27 stars 2 forks source link

No CORS mode or JSONP support #21

Closed graypegg closed 8 years ago

graypegg commented 8 years ago

The JSON API should support Cross-Origin Requests because the API is potentially useful in the browser. JSONP support would also work.

Recreating the issue:

Try running the following in your browser's JS Console on a site other than https://iconbin.com/

fetch("https://iconbin.com/api/instagram.com")
    .then(function(res){
        return res.json();
    })
    .then(function(data){
        // `data` will never be resolved due to
        // the Cross-Origin Request policy.
        console.log(data);
    })

Solution

Add the CORS headers to the routes in the Express app. Then requests can be made like this

fetch("https://iconbin.com/api/instagram.com", { mode: "cors" })
    .then(function(res){
        return res.json();
    })
    .then(function(data){
        // `data` is resolved!
        console.log(data);
    })
psolbach commented 8 years ago

Good point. As I've added the API code now you're very welcome to fix this yourself if you like.