question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having trouble with CORS.

Investigative information

Please provide the following:

  • Timestamp: 2:14PM November 14th 2018
  • Function App name: recommender-test-4
  • Function name(s) (as appropriate): GetRecommendations

Repro steps

Provide the steps required to reproduce the problem:

  1. Deployed this app.
  1. Want to access from another app:
axios.get(`https://recommender-test-4.azurewebsites.net/api/GetRecommendations?code=GHvuTHyv5jd5EsTK44lwQgwTQEwk2PbI6zkS7rugaVVjM7dInG4SQA==`)
            .then(res => {
                const data = res.data.data.children.map(obj => obj.data);
                console.log(`Response Data: ${data}` );
            }).catch((err, res) => {
                console.log(err);
            });
  1. Cors error: image

  2. Add * cors rule: image

Expected behavior

At this point I would expect to see the error disappear.

Actual behavior

Request is going through, but error remains. So I can’t actually get the body out of the response. image

image

Known workarounds

Apparently there is a way to add some headers directly to func.HttpResponse to make this work? But I can’t figure it out…

I could do something like this in JavaScript?

module.exports = function (context, req) {
    context.res = {
        status: 200,
        body: "Hello " + req.query.name,
        headers: {
            'Content-Type': 'text/plain',
            'Access-Control-Allow-Origin': 'True'
        }
    };
    context.done();
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
asavaritayalcommented, Nov 15, 2018

Here’s the API reference for azure.functions.HttpResponse - https://docs.microsoft.com/en-us/python/api/azure-functions/azure.functions.httpresponse?view=azure-python

You can provide a dict for the headers field.

Here’s an example:

headers  = {"Ocp-Apim-Subscription-Key": subscription_key,
                "Content-Type": "application/octet-stream"}
response = func.HttpResponse(headers=headers)
return response

1reaction
bowdenk7commented, Nov 15, 2018

Awesome thanks. Tacking on the below headers worked for me.

headers  = {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "Get, Post, Options"
}

This will work fine for the demo, but it feels like this is redundant with setting CORS policies in portal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CORS errors - HTTP - MDN Web Docs - Mozilla
If the CORS configuration isn't setup correctly, the browser console will present an error like "Cross-Origin Request Blocked: The Same ...
Read more >
Understanding and Resolving CORS Error - Contentstack
CORS is implemented on the server-side; it cannot be reconfigured on the client-side. The CORS behavior, commonly termed as CORS error, is a...
Read more >
3 Ways to Fix the CORS Error — and How the Access-Control ...
The error stems from a security mechanism that browsers implement called the same-origin policy. The same-origin policy fights one of the most ...
Read more >
What Is a CORS Error and How to Fix It (3 Ways) - Bannerbear
As a CORS error occurs when the external API server doesn't return the HTTP headers required by the CORS standard, you can add...
Read more >
CORS errors and how to solve them - Topcoder
Or, your API fails and shows a CORS error in the console. · This happens because the same-origin policy is part of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found