Cenxui Cenxui - 17 days ago 6
Javascript Question

Api Gateway cannot allow Access-Control-Allow-Origin

This url is in AWS API Gateway with method get and stage is well deployed.
And I enabled CORS following the aws document.

Here are my steps to enable CORS.

-Resource->action->enable CORS->
default setting ->enable CORS and replacing the CORS headers.
There is no error log in CORS result.

I am not a profesional web developer and my browser is safari.

Here is my code to query "http://my.com"

function request(idex) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", "http://my.com", true);
xmlHttp.send(null);}


The console print the error :
XMLHttpRequest cannot load "http://my.com" Origin http://example.com is not allowed by Access-Control-Allow-Origin.

If there are some mistakes in javascript request or in API Gateway deploy?

Answer

After consulting and trying each methods, I found that the error. We can not deploy our api before enabling CORS. All the setting about the header and CORS must be setting before deploying. But the API Gateway does not block this setting or showing any error dialog. API Gateway will not change the header even your setting process shows success.