Access Control Allow Origin on Netlify 2.0 Functions

2 is host2 - the server
1 is host1 - client

  • Build problems? No

The better the post - the faster the answer.

code extract:
return new Response(JSON.stringify(res), { headers: { 'access-control-allow-origin': '*' } } )

Log on Server and local:
Function utils has returned an error: Unexpected end of JSON input

I call from a client on host1 a function “utils” on host2.
In the old lambda world, the final return { headers: { 'access-control-allow-origin': '*' } } in the handler function worked.

Here the code I have written here seems to work for the dev-author, but maybe with more to do. Any help?

Sounds like your res is not a valid JSON. Doesn’t looks like this has anything to do with the CORS headers.

I do ntl functions:invoke utils --port 80 without cross origin (same client same server) and it works, so the json res var should be ok.

It outputs in the browser with cross origin localhost:1313:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)

with cross origin t–
[Error] Origin is not allowed by Access-Control-Allow-Origin. Status code: 502

Should I deploy to prod and put the logs here?
With ntl deploy --alias t I cannot see the logs.

How are you even invoking Netlify Functions from :1313? That port is not handled by CLI and unless you somehow forward the requests to :8888 that would simply throw a 404.

This error likely also means that your Response() constructor failed due to (my guess) JSON.stringify() not working on a valid JSON. Since the Response didn’t get correctly constructed, it probably threw the error you mentioned.

Without a working reproduction, I can only continue guessing. Let me know if you can put something together for me to test this live.