Access-Control-Allow-Origin not working

1,023 views
Skip to first unread message

Bradley J. Gibby

unread,
Feb 8, 2017, 9:03:25 AM2/8/17
to gce-discussion
Hi everyone,

I feel like a newbie here but I'm trying to run a simple AJAX request from a browser to access a GCF and Chrome is reporting:

XMLHttpRequest cannot load https://us-central1-bustling-opus-830.cloudfunctions.net/Authenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://beast.reachboarding.com.au' is therefore not allowed access.

I've got a function called Authenticate (as shown above) that's using a bucket called:


I've used gsutil to set CORS using the following JSON file:

[{
"responseHeader": ["Content-Type"],
"method": ["GET", "HEAD", "DELETE", "OPTIONS"],
"maxAgeSeconds": 3600
}]

With the following command:

gsutil cors set cors.json gs://bustling-opus-830.appspot.com/

And then get the following output from the corresponding get command:

gsutil cors get gs://bustling-opus-830.appspot.com/

[{"maxAgeSeconds": 3600, "method": ["GET", "HEAD", "DELETE", "OPTIONS"], "origin": ["https://beast.reachboarding.com.au", "*"], "responseHeader": ["Content-Type"]}]

I'm using the default example code that's provided when you create a new function as stated below:

/**
 * Responds to any HTTP request that can provide a "message" field in the body.
 *
 * @param {!Object} req Cloud Function request context.
 * @param {!Object} res Cloud Function response context.
 */
exports.helloWorld = function helloWorld(req, res) {
// Example input: {"message": "Hello!"}
if (req.body.message === undefined) {
// This is an error case, as "message" is required.
res.status(200).send('No message defined!');
} else {
// Everything is okay.
console.log(req.body.message);
res.status(200).send(req.body.message);
}
};

And a simple HTML with the following Javascript:

$.ajax({
type: "POST",
data: {
message: 'Testing'
},
dataType: 'json', 
success: function (response) {
console.log(response);
},
error: function (xhr, status) {
console.log(xhr);
}
});

Which is causing the error.

In my DEV console I can see the network request go through. Here are the HTTP Response Headers I'm getting are:

cache-control:private
content-encoding:gzip
content-length:27
content-type:text/html; charset=utf-8
date:Wed, 08 Feb 2017 03:45:50 GMT
etag:W/"7-274e639a"
function-execution-id:azc1zqfb1tq8
server:Google Frontend
status:200
vary:Accept-Encoding
x-cloud-trace-context:70e08d634a9644c32530326de0471a64;o=1
x-cloud-trace-context:70e08d634a9644c32530326de0471a64
x-powered-by:Express

I would have expected to see the Access-Control-Allow-Origin header within the Response Headers to indicate that it was allowing * but I'm definitely not seeing it.

The crazy thing though is that when I look at the Network item and click on Response I get:

Testing

Which suggests that all things being equal, it ACTUALLY ran!

I apologise if this has been answered before but I've searched for as many different keywords and nothing seems to have solved my problem. I thought a fresh pair of eyes on the issue (and some decent expertise) would help.

Thanks in advance!

Faizan (Google Cloud Support)

unread,
Feb 8, 2017, 4:44:59 PM2/8/17
to gce-discussion
Hello Bradley,

I would recommend posting this on Google Cloud Platform issue tracker, as this forum is for Compute Engine general discussion. Make sure to include detailed information including your HAR file with the request and response. This will help the team debug the issue further.

I hope that helps.

Faizan
Reply all
Reply to author
Forward
0 new messages