将JSON数据附加到POST请求 [英] Append JSON data to POST request
问题描述
我有一个正确提交的AJAX表单,并将完整的模型发送到控制器中.我想要的是添加一个与请求一起发送的JSON.我设法拦截了这样的POST:
I have an AJAX form that submits correctly and that sends a complete model into the controller. What I want is to add a JSON to be sent with the Request. I have managed to intercept the POST like this:
$(document).ready(function() {
$("form").submit(function(e) {
if (e.originalEvent.explicitOriginalTarget.id == "submit") {
}
});
我不知道如何发送JSON数据,同时还保留最初在表单提交时发送的数据.我曾想过要添加一个隐藏字段,将其值设置为JSON字符串,然后在服务器上对其进行反序列化,但这似乎是错误的.
What I don't know is how to send my JSON data, while also keeping the data sent initially on the form submission. I had a thought at adding a hidden field, setting its value to the JSON string and then de-serializing it on the server, but that seems rather wrong.
推荐答案
如果无法使用AJAX,则必须使用隐藏字段将JSON数据存储在表单中.否则,您的JSON将永远不会发送到服务器. HTML规范明确规定了规则:提交表单后,只有表单内输入字段中包含的值才发送到服务器.
If you cannot use AJAX, you will have to use a hidden field to store the JSON data inside the form. Otherwise your JSON will never be sent to the server. The HTML specification clearly states the rules: only the values contained in input fields inside the form are sent to the server when this form is submitted.
这篇关于将JSON数据附加到POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!