AWS Lambda + API 网关 POST 错误
AWS Lambda + API Gateway POST Error
我有一个简单的 Lambda 函数,它接收一些文本,returns 该文本作为响应。我将它与 AWS API 网关连接起来,并在他们的控制台和 Postman 上对其进行了测试。我启用了CORS,Postman上的headers似乎是对的,Access-Control-Allow-Origin
设置为*.
这是 Postman 结果:
我无法在本地运行它,所以我决定在这里托管一个静态页面:
https://smileyfacetest.firebaseapp.com/
test = {"text": ":)"}
$.post( "https://pq8thdrp0a.execute-api.us-west-2.amazonaws.com/dev", test)
.done(function( data ) {
console.log(data);
});
但仍然报错:
XMLHttpRequest cannot load https://pq8thdrp0a.execute-api.us-west-2.amazonaws.com/dev. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://smileyfacetest.firebaseapp.com' is therefore not allowed access. The response had HTTP status code 400.
我在前端做错了什么吗?如果它在 Postman 上工作,并且我在 API 的端点上启用了 CORS,我不明白为什么它应该抛出错误。任何帮助将不胜感激!
如果你想在 Postman 中尝试,这里是 API 端点:
我测试了你的电话并且它有效。但是,您需要在 jQuery 调用中将内容类型设置为 JSON:
$.ajax({
type: 'POST',
url: '/form/',
data: '{"name":"jonas"}', // or JSON.stringify ({name: 'jonas'}),
success: function(data) { alert('data: ' + data); },
contentType: "application/json",
dataType: 'json'
});
我从 How can I use JQuery to post JSON data?
中截取了这个片段
我有一个简单的 Lambda 函数,它接收一些文本,returns 该文本作为响应。我将它与 AWS API 网关连接起来,并在他们的控制台和 Postman 上对其进行了测试。我启用了CORS,Postman上的headers似乎是对的,Access-Control-Allow-Origin
设置为*.
这是 Postman 结果:
我无法在本地运行它,所以我决定在这里托管一个静态页面:
https://smileyfacetest.firebaseapp.com/
test = {"text": ":)"}
$.post( "https://pq8thdrp0a.execute-api.us-west-2.amazonaws.com/dev", test)
.done(function( data ) {
console.log(data);
});
但仍然报错:
XMLHttpRequest cannot load https://pq8thdrp0a.execute-api.us-west-2.amazonaws.com/dev. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://smileyfacetest.firebaseapp.com' is therefore not allowed access. The response had HTTP status code 400.
我在前端做错了什么吗?如果它在 Postman 上工作,并且我在 API 的端点上启用了 CORS,我不明白为什么它应该抛出错误。任何帮助将不胜感激!
如果你想在 Postman 中尝试,这里是 API 端点:
我测试了你的电话并且它有效。但是,您需要在 jQuery 调用中将内容类型设置为 JSON:
$.ajax({
type: 'POST',
url: '/form/',
data: '{"name":"jonas"}', // or JSON.stringify ({name: 'jonas'}),
success: function(data) { alert('data: ' + data); },
contentType: "application/json",
dataType: 'json'
});
我从 How can I use JQuery to post JSON data?
中截取了这个片段