我有一个带有按钮的HTML表单,它允许我在"中发送HTTP请求;text/plain";或";application/x-www-form-urlencoded";总体安排
text/plain:
stateMachineArn=arn:aws:states:us-east-1:0111111165:stateMachine:MyStateMachine
name=ExecutionName
first_name=test
last_name=test2
application/x-www-form-urlencoded:
stateMachineArn=arn:aws:states:us-east-1:0111111165:stateMachine:MyStateMachine&name=ExecutionName&first_name=test&last_name=test2
但AWS API网关默认情况下只接收JSON格式(必须转义"输入"(
{
"input": "{"first_name" : "test","last_name" : "test2"}",
"name": "ExecutionName",
"stateMachineArn": "arn:aws:states:us-east-1:0111111165:stateMachine:MyStateMachine"
}
如何将任一格式转换为上述JSON?
如果您有这样的表单:
<form>
<input type="stateMachineArn" name="stateMachineArn" id="stateMachineArn" />
<input type="name" name="name" id="name" />
<label for="first_name">First name</label>
<input type="first_name" name="first_name" id="first_name" />
<label for="last_name">Last name</label>
<input type="last_name" name="last_name" id="last_name" />
<button type="submit">Submit</button>
</form>
你可以像这样在js中处理提交,并像以前一样记录响应——不知道你是不是在这样做:
function handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);
const value = Object.fromEntries(data.entries());
console.log({ value });
}
const form = document.querySelector('form');
form.addEventListener('submit', handleSubmit);
在这一点上,如果您想在将数据对象发送到某个地方之前将其转换为json,或者只是为了记录它,您尝试过吗?
console.log(JSON.stringify(data));
还是这个?
console.log(JSON.stringify(value ));
编辑
发送请求:
$.ajax({
type: "POST",
url: "/webservices/PodcastService.asmx/CreateMarkers",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
error: function(errMsg) {
alert(errMsg);
}
});