正确发送HTTP发布请求 [英] Sending a HTTP Post Request correctly
问题描述
我有一个简单的AWS Lambda函数,它必须接收HTTP Post数据并打印它:
I have a simple AWS Lambda function that must receive HTTP Post data and print it:
AWS Lambda函数
AWS Lambda function
var aws = require('aws-sdk');
var querystring = require('querystring');
var ses = new aws.SES();
exports.handler = function(event, context) {
console.log("Incoming: ", event);
};
我的HTML表单:
My HTML form:
<form action="http://myawsfuncurl" method="POST">
<input type="textarea" width="200" height="100" value="Hello"><br>
<input type="submit" value="Send">
</form>
控制台只是打印:来电:{}
没有收到任何数据。
The console simply prints: Incoming: {}
No data is received.
但是,当我通过java代码发送HTTP Post时,这个工作正常:
However, this works fine when i send HTTP Post via a java code:
Java代码
Java Code
URL obj = new URL("http://myawsfuncurl");
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String payload = "hello";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(payload);
wr.flush();
wr.close();
如何通过HTML表单使其工作?
推荐答案
尝试通过http POST表单发送时遇到了同样的问题。
I had the same issue trying to send it via http POST form.
我通过发送JSON数据到函数的Ajax请求发送它。
I got around this by sending it via an Ajax request which sends JSON data to the function.
这样的事情应该会有所帮助。如果你把它放在html页面上的脚本标签中。
Something like this should hopefully help. If you put it in a script tag on your html page.
<script>
$.support.cors = true;
$('#id_of_form').submit(function(event){
event.preventDefault();
$.ajax(
{
type: 'POST',
url: 'http://myawsfuncurl',
dataType: 'json',
data: {
ParamA: $('#id_of_textarea").text()
},
error: function(obj, msg, exc){},
success: function(data){
alert('complete');
}
}
)
return false;
});
</script>
这篇关于正确发送HTTP发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!