如何将表单数据和非表单数据(json,javascript数组)发布到服务器 [英] How do I post both form data and non-form data(json, javascript array) to server

查看:103
本文介绍了如何将表单数据和非表单数据(json,javascript数组)发布到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个HTML表单,上面有一些输入元素 - 比如姓名,国家/地区。我有另一张桌子,显示食物偏好。当用户添加食物偏好时,它们会被添加到后台的数组/ JSON中(使用javascript)并显示在表格中。

仅将表单发布到服务器是好的,但同时发布两者表格和一系列偏好对我来说很难。我正在使用asp.net核心。我究竟做错了什么?谢谢



我尝试了什么:



我尝试使用jquery ajax但是数组不会发送到服务器。总是发送null。

I made a an HTML form which has some input elements on it- say name, country. I have another table which displays food preferences. when the user adds a food preference, they are added to an array/JSON in the background (using javascript) and displayed in the table.
Posting only the form to server is Ok, but posting both the form and the array of preferences is difficult for me. I'm using asp.net core. What am I doing wrong? thanks

What I have tried:

I tried using jquery ajax but the array is not sent to the server. null is always sent.

推荐答案

这是一个你应该能够经常重用的逻辑:



什么时候生成JSON,使用DOM将其存储在表单中。现在它将使用表单数据的其余部分转到服务器。
Here's a logic you should be able to reuse often:

When you generate your JSON, use the DOM to store it in your form. Now it will go to the server with the remainder of the form's data.
<include type='hidden' name='json' id='json' value='the JSON data string'>

您可以使用隐藏的输入元素做很多好事。

You can do lots of nice things with the hidden input element.


这篇关于如何将表单数据和非表单数据(json,javascript数组)发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆