确保在AWS Lambda中正确设置了CORS(跨域资源共享)选项。
在Axios请求中设置headers和responseType选项,以使Axios能够正确解析响应。例如:
axios.get('https://example.com/api', {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
responseType: 'json'
})
.then(response => {
console.log(response.data)
})
.catch(error => {
console.log(error)
})
exports.handler = async (event) => {
try {
// Do your AWS Lambda code here
const response = {
statusCode: 200,
body: JSON.stringify({ message: 'Success' })
}
return response
} catch (error) {
console.error(error)
const response = {
statusCode: 500,
body: JSON.stringify({ message: 'Error' })
}
return response
}
};
上一篇:AWSLambda响应缓慢
下一篇:AWSLambda许可事件性