Ajax Call Using Jquery To Aws Lambda Function In Python
I'm trying to get my head around ajax calls and aws lambda, but I've been struggling for hours with the most simple example I could think of: just to have javascript / jquery do a
Solution 1:
You are missing a comma after type: 'POST'
$.ajax(
{
url: 'https://npvkf9jnqb.execute-api.us-east-1.amazonaws.com/v1',
type:'POST',
dataType: 'text',
success: function(data) {
window.alert(data);
}
});
Solution 2:
I will summarize the solution I found in thee steps for the error:
Failed to load https://npvkf9jnqb.execute-api.us-east-1.amazonaws.com/v1: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. in case anyone has the same problem.
1 - Cors has to be enabled and it is necessary to specify 'Access-Control-Allow-Origin':'*'
This is by default. This setting can be found in AWS API Gateway settings.
2 -The Ajax call should contain the header 'Access-Control-Allow-Origin':'*'
. This is inside the html file.
$.ajax(
{
url: 'https://npvkf9jnqb.execute-api.us-east-1.amazonaws.com/v1',
headers: {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'
},
crossDomain: true,
type:'GET',
dataType: 'text',
success: function(data)
{
window.alert(data);
}
}); `
3 - The Lambda function also needs to return the header 'Access-Control-Allow-Origin':'*'
. This should be done in AWS Lambda.
deflambda_handler(a, b):
return({
"isBase64Encoded": True,
"statusCode": 200,
"headers": { 'Access-Control-Allow-Origin': '*'},
"body": "blahhh"
})
Post a Comment for "Ajax Call Using Jquery To Aws Lambda Function In Python"