I am running into another CORS issue with Serverless and AWS. Seems I need to tell it specifically to allow a PUT method, but I am not sure where this code goes. I have the following code for my Lambda function:
module.exports.update = (event, context, callback) => {
const timestamp = new Date().getTime();
const data = JSON.parse(event.body);
if (typeof event.pathParameters.timeoffgroupid !== 'string') {
console.error('Validation Failed');
callback(new Error('Couldn\'t update the todo item.'));
return;
}
const params = {
TableName: 'TimeOffGroup',
Key: {
timeoffgroupid: event.pathParameters.timeoffgroupid,
},
ExpressionAttributeValues: {
':timeOffGroup': data.timeOffGroup,
':timeOffGroupColor': data.timeOffGroupColor,
':dateModified': timestamp
},
UpdateExpression: 'SET timeOffGroup = :timeOffGroup, timeOffGroupColor = :timeOffGroupColor, dateModified = :dateModified',
ReturnValues: 'ALL_NEW',
};
dynamoDb.update(params, (error, result) => {
if (error) {
console.error(error);
callback(new Error('Couldn\'t update the todo item.'));
return;
}
const response = {
statusCode: 200,
body: JSON.stringify(result.Attributes),
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true
}
};
callback(null, response);
});
};
The error I am getting is:
XMLHttpRequest cannot load https://pwqlomgq89.execute-api.us-east-1.amazonaws.com/dev/timeoffgroup/7d463800-0935-11e7-b618-4b1d72ddca8e?timeOffGroup=Holiday+edited. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 502. angular.js:14328 Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"PUT","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","data":{},"url":"https://pwqlomgq89.execute-api.us-east-1.amazonaws.com/dev/timeoffgroup/7d463800-0935-11e7-b618-4b1d72ddca8e","params":{"timeOffGroup":"Holiday edited"},"headers":{"Accept":"application/json, text/plain, /","Content-Type":"application/json;charset=utf-8"}},"statusText":""}
I have followed all of the calls on serverless tutorials to get the CORS to work. But it seems I got the get and create to work, but the PUT command is not working. Is there some place I need to still set the update command to allow PUT for cross domain?
Looks like I might need more here for a PUT method access: headers:
{
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true
}