使用 JSON 格式的 POST 方法 web api 将动态输入提交到数据库

Posted

技术标签:

【中文标题】使用 JSON 格式的 POST 方法 web api 将动态输入提交到数据库【英文标题】:Submitting dynamic inputs into database using POST method web api in JSON format 【发布时间】:2017-09-17 21:46:24 【问题描述】:

我目前正在开发一个动态表单构建器(允许用户创建他们想要的任何表单)。由于它是动态的,我无法预测输入将是什么,因为问题可能是复选框等。因此,我想知道您如何使用 JSON 格式的 post 方法 web api 将表单输入准确提交到数据库。此外,它必须能够基于 tag 创建一个新表。对不起,如果它令人困惑,我对这个领域很陌生,没有任何帮助。

下面的代码是xml文件,xslt将在网页上显示为html

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<form>
  <display_name>In Store Hub Trooper Feedback Form</display_name>
  <form_theme>SkyBlue</form_theme>
  <show_score>True</show_score>
  <calculate>True</calculate>
  <db_table_name>form_for_hub_trooper_in_store_feedback_form_20160829174321</db_table_name>
  <fieldset>

    <field required="Y" is_admin_field="N">
      <question_title>Date of Audit</question_title>
      <type>DateOfAudit</type>
      <db_field_name>date_of_audit</db_field_name>
      <additional_comment/>
    </field>
    <field required="Y" is_admin_field="N">
      <question_title>Name of Auditor</question_title>
      <type>TextField</type>
      <db_field_name>name_of_auditor</db_field_name>
      <db_field_length>100</db_field_length>
      <db_field_type>string</db_field_type>
      <additional_comment/>
    </field>
    <field required="Y" is_admin_field="N">
      <question_title>Store Location</question_title>
      <type>TextField</type>
      <db_field_name>store_location</db_field_name>
      <db_field_type>string</db_field_type>
      <additional_comment/>
    </field>
    <field required="Y" is_admin_field="N">
      <question_title>Name of Staff</question_title>
      <type>TextField</type>
      <db_field_name>name_of_staff</db_field_name>
      <db_field_length>100</db_field_length>
      <db_field_type>string</db_field_type>
      <additional_comment/>
    </field>
    <group name="Basic Service Skills">
      <section name="Greet" db_field_name="greet_score">
        <field required="Y" is_admin_field="N">
          <question_title>Welcome customer with standard greeting</question_title>
          <type>List</type>
          <db_field_name>welcome_customer_with_standard_greeting_greet</db_field_name>
          <options>
            <item score="1" id="Welcome customer with standard greetingyes" extratext="N">Yes</item>
            <item score="0" id="Welcome customer with standard greetingno" extratext="N">No</item>
          </options>
          <db_field_length>3</db_field_length>
          <additional_comment/>
        </field>
      </section>
      <section name="Service Interaction" db_field_name="service_interaction_score">
        <field required="Y" is_admin_field="N">
          <question_title>Ask for customer's name politely</question_title>
          <type>List</type>
          <db_field_name>
            ask_for_customers_name_politely_service_interaction
          </db_field_name>
          <options>
            <item score="2" id="Ask for customer's name politelyyes" extratext="N">Yes</item>
            <item score="0" id="Ask for customer's name politelyno" extratext="N">No</item>
            <item score="2" id="Ask for customer's name politelyno with reason" extratext="Y" >No with reason</item>

          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Understand and meet customer's needs</question_title>
          <type>List</type>
          <db_field_name>
            understand_and_meet_customers_needs_service_interaction
          </db_field_name>
          <options>
            <item score="2" id="Understand and meet customer's needsyes" extratext="N">Yes</item>
            <item score="0" id="Understand and meet customer's needsno" extratext="N">No</item>
          </options>
          <db_field_length>3</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Active listening</question_title>
          <type>List</type>
          <db_field_name>active_listening_service_interaction</db_field_name>
          <options>
            <item score="1" id="Active listeningyes" extratext="N">Yes</item>
            <item score="0" id="Active listeningno" extratext="N">No</item>
          </options>
          <db_field_length>3</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Address customer by name</question_title>
          <type>List</type>
          <db_field_name>address_customer_by_name_service_interaction</db_field_name>
          <options>
            <item score="3" id="Address customer by nameyes" extratext="N">Good</item>
            <item score="1" id="Address customer by nameavg" extratext="N">Avg</item>
            <item score="0"  id="Address customer by namepoor" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
      </section>
    </group>
    <group name="Security / PDPA">
      <section name="Control" db_field_name="control_score">
        <field required="Y" is_admin_field="N">
          <question_title>Verify customer's details when necessary</question_title>
          <type>List</type>
          <db_field_name>verify_customers_details_when_necessary_control</db_field_name>
          <options>
            <item score="2" id="Verify customer's details when necessaryyes" extratext="N">Yes</item>
            <item score="0" id="Verify customer's details when necessaryno" extratext="N">No</item>
            <item score="2"  id="Verify customer's details when necessarynowithreason" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Display control over situation</question_title>
          <type>List</type>
          <db_field_name>display_control_over_situation_control</db_field_name>
          <options>
            <item score="1" id="xzcasd" extratext="N">Yes</item>
            <item score="0" id="fshfusai" extratext="N">No</item>
            <item score="1" id="gcvilhuf8" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Proactive in probing for more details</question_title>
          <type>List</type>
          <db_field_name>proactive_in_probing_for_more_details_control</db_field_name>
          <options>
            <item score="1" id="d8yfiwgdu" extratext="N">Yes</item>
            <item score="0" id="sdjfisdgfs" extratext="N">No</item>
            <item score="1" id="dsifyw" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
      </section>
      <section name="Closing" db_field_name="closing_score">
        <field required="Y" is_admin_field="N">
          <question_title>Pre-closing</question_title>
          <type>List</type>
          <db_field_name>preclosing_closing</db_field_name>
          <options>
            <item score="1" id="sdfietr" extratext="N">Yes</item>
            <item score="0" id="dfghvmdxsrw" extratext="N">No</item>
            <item score="1" id="jnvswoeirytu" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Thank customer with standard greeting</question_title>
          <type>List</type>
          <db_field_name>thank_customer_with_standard_greeting_closing</db_field_name>
          <options>
            <item score="1" id="siyfh" extratext="N">Yes</item>
            <item score="0" id=" jhvcb" extratext="N">No</item>
            <item score="1" id="hgfcfdsg" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
      </section>
    </group>
    <group name="Temporary Absence">
      <section name="Explain for Absence" db_field_name="explain_for_absence_score">
        <field required="Y" is_admin_field="N">
          <question_title>Explain reason for temporary absence</question_title>
          <type>List</type>
          <db_field_name>
            explain_reason_for_temporary_absence_explain_for_absence
          </db_field_name>
          <options>
            <item score="3" id="dfihsvbiw" extratext="N">Good</item>
            <item score="1" id="yufeigvb" extratext="N">Avg</item>
            <item score="0" id="nfcjnvxiqeghe" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Thank customer for waiting upon return</question_title>
          <type>List</type>
          <db_field_name>
            thank_customer_for_waiting_upon_return_explain_for_absence
          </db_field_name>
          <options>
            <item score="3" id="fdssdfsdxcvx" extratext="N">Good</item>
            <item score="1" id="sdfsfsdxvxbn" extratext="N">Avg</item>
            <item score="0" id="fguydgfdsifdygfsbvxkvx" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
      </section>
      <section name="Transfer Techniques" db_field_name="transfer_techniques_score">
        <field required="Y" is_admin_field="N">
          <question_title>Usher/direct customer when necessary</question_title>
          <type>List</type>
          <db_field_name>
            usherdirect_customer_when_necessary_transfer_techniques
          </db_field_name>
          <options>
            <item score="1" id="sdfusdhfusfhsfsfhsgsd" extratext="N">Yes</item>
            <item score="0" id="euifhufdgafifort546" extratext="N">No</item>
            <item score="1" id="dsufidhsfbvsao87866484" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
      </section>
    </group>
    <group name="Impression">
      <section name="Essential Courtesies" db_field_name="essential_courtesies_score">
        <field required="Y" is_admin_field="N">
          <question_title>Friendly and pleasant disposition</question_title>
          <type>List</type>
          <db_field_name>
            friendly_and_pleasant_disposition_essential_courtesies
          </db_field_name>
          <options>
            <item score="3" id="duiahsadhsidhaduiacbx" extratext="N">Good</item>
            <item score="1" id="dfsfstrtw5432" extratext="N">Avg</item>
            <item score="0" id="auidsheauhfiuaiuhae23422" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Eye contact / body language</question_title>
          <type>List</type>
          <db_field_name>eye_contact_body_language_essential_courtesies</db_field_name>
          <options>
            <item score="3" id="aduyigta7e8o7wqtr3q785tr387q" extratext="N">Good</item>
            <item score="1" id="34242dsfgbvcsdfsbg" extratext="N">Avg</item>
            <item score="0" id="uhrwerwh56484sfs6435" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Patience</question_title>
          <type>List</type>
          <db_field_name>patience_essential_courtesies</db_field_name>
          <options>
            <item score="3" id="sdf8iohfsoihohs54352342dfsf" extratext="N">Good</item>
            <item score="1" id="ufishduheorq03291df5s64" extratext="N">Avg</item>
            <item score="0" id="dsfidosjfdsifdshfspfs543242sda" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Display human touch</question_title>
          <type>List</type>
          <db_field_name>display_human_touch_essential_courtesies</db_field_name>
          <options>
            <item score="3" id="dsfiuysfhgoisduf9328473248937432984" extratext="N">Good</item>
            <item score="1" id="audhasduahdadiua7324632" extratext="N">Avg</item>
            <item score="0" id="uihfehrqueriqfgqe8942732" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Choice of words</question_title>
          <type>List</type>
          <db_field_name>choice_of_words_essential_courtesies</db_field_name>
          <options>
            <item score="3" id="duriyrweorewyr798wer" extratext="N">Good</item>
            <item score="1" id="ewr7ew8rytwe87rewyt8742873432g38" extratext="N">Avg</item>
            <item score="0" id="3894y242849y2hdsu9fdsh8" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Rate of speech / enunciation</question_title>
          <type>List</type>
          <db_field_name>rate_of_speech_enunciation_essential_courtesies</db_field_name>
          <options>
            <item score="3" id="uirhewurwb42342uibfds" extratext="N">Good</item>
            <item score="1" id="ajdnadjahd9o7342" extratext="N">Avg</item>
            <item score="0" id="ufdahdoisad9876983473" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Bad habits</question_title>
          <type>List</type>
          <db_field_name>bad_habits_essential_courtesies</db_field_name>
          <options>
            <item score="3" id="aoidajoidjaw4253453" extratext="N">Good</item>
            <item score="1" id="erwoiuhwroiuwher9842" extratext="N">Avg</item>
            <item score="0" id="ewrwihrewiurewr783432" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
      </section>
      <section name="Grooming" db_field_name="grooming_score">
        <field required="Y" is_admin_field="N">
          <question_title>Well-groomed</question_title>
          <type>List</type>
          <db_field_name>wellgroomed_grooming</db_field_name>
          <options>
            <item score="3" id="duhdaosp898342424" extratext="N">Good</item>
            <item score="1" id="aduhfuahbulhw432" extratext="N">Avg</item>
            <item score="0" id="udfhaoiauhyy987132" extratext="N">Poor</item>
          </options>
          <db_field_length>4</db_field_length>
          <additional_comment/>
        </field>
      </section>
    </group>
    <group name="Business Competency">
      <section name="Business Knowledge" db_field_name="business_knowledge_score">
        <field required="Y" is_admin_field="N">
          <question_title>Give correct information for products or services</question_title>
          <type>DropDownList</type>
          <db_field_name>
            give_correct_information_for_products_or_services_business_knowledge
          </db_field_name>
          <options>
            <item score="2" id="aduoiahsiuh4968586458496514" extratext="N">Yes</item>
            <item score="0" id="suihfsiufho7y467898" extratext="N">No</item>
          </options>
          <db_field_length>3</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N"  gotCode="Y">
          <question_title>What programming language is used below?</question_title>
          <codeQ>
     correct information for products or services What programming language is used below?
         </codeQ>


          <type>List</type>
          <db_field_name>
            retrieve_relevant_information_efficiently_business_knowledge
          </db_field_name>
          <options>
            <item score="2" id="aduhsadiuah33y9o2313213" extratext="N">Yes</item>
            <item score="0" id="easdeqw412312" extratext="N">No</item>
          </options>
          <db_field_length>3</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Follow correct processes &amp; procedures</question_title>
          <type>List</type>
          <db_field_name>
            follow_correct_processes_procedures_business_knowledge
          </db_field_name>
          <options>
            <item score="2" id="eqeqwdasavcxx523423" extratext="N">Yes</item>
            <item score="0" id="fdjfsoifjsoijoij7878787878785634543" extratext="N">No</item>
          </options>
          <db_field_length>3</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Update customer incident in system</question_title>
          <type>List</type>
          <db_field_name>
            update_customer_incident_in_system_business_knowledge
          </db_field_name>
          <options>
            <item score="2" id="dadsaudhaduahuirhewrhiewur3243243241231465452131" extratext="N">Yes</item>
            <item score="0" id="dasbdaihgqiuhg4321313132131" extratext="N">No</item>
            <item score="2" id="wsjbsajbjnvu5235234675464-=" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
      </section>
      <section name="Provide Solution" db_field_name="provide_solution_score">
        <field required="Y" is_admin_field="N">
          <question_title>Offer alternatives when needed</question_title>
          <type>List</type>
          <db_field_name>offer_alternatives_when_needed_provide_solution</db_field_name>
          <options>
            <item score="1" id="asdadnbjnbvxxofi435435343" extratext="N">Yes</item>
            <item score="0" id="idfjsoifjdofsjfojfsfjsdfsa;jxjvnbvc,mnbj" extratext="N">No</item>
            <item score="1" id="sdlakda4564ad4a64w6e4wq5eq1eq" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
      </section>
      <section name="Upsell / Retention" db_field_name="upsell_retention_score">
        <field required="Y" is_admin_field="N">
          <question_title>Upsell value-added services / upgrades</question_title>
          <type>List</type>
          <db_field_name>
            upsell_valueadded_services_upgrades_upsell_retention
          </db_field_name>
          <options>
            <item score="2" id="sad1sad3sa1da5sda15a1d6awe4q8e623" extratext="N">Yes</item>
            <item score="0" id="dfdsfbhu34b2bw4b234" extratext="N">No</item>
            <item score="2" id="4u32h432u4h24j24h24j24h2b424b" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
        <field required="Y" is_admin_field="N">
          <question_title>Probe reasons for termination</question_title>
          <type>MultiLineTextField</type>
          <db_field_name>probe_reasons_for_termination_upsell_retention</db_field_name>

          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
      </section>
      <section name="Resolution" db_field_name="resolution_score">
        <field required="N" is_admin_field="N">
          <question_title>First contact resolution</question_title>
          <type>MultiList</type>
          <db_field_name>first_contact_resolution_resolution</db_field_name>
          <options>
            <item score="2" id="b44b234j23bjdf9u9807" extratext="N">Yes</item>
            <item score="0" id="jbfejwbj53j423b4j2b5" extratext="N">No</item>
            <item score="2" id="ewjrhwueh6u235h234bjbr23242" extratext="Y">No with reason</item>
          </options>
          <db_field_length>14</db_field_length>
          <additional_comment/>
        </field>
      </section>
    </group>
    <group name="Rate This Form!">
      <field is_admin_field="N" required="Y">
        <question_title>How would you rate this form?</question_title>
        <type>Drop</type>
        <db_field_name>How_would_you_rate_this_form_?</db_field_name>
      </field>
      <db_field_length>8</db_field_length>
      <additional_comment/>
    </group>
    <fail marks="5"/>
    <pass marks="10"/>
    <great marks="15"/>
  </fieldset>
</form>

【问题讨论】:

首先我确保动态控件只会在特定的 div 中创建。然后使用 jquery 我得到了这个 div 中的所有控件,并根据类型编写了代码来处理它们。 【参考方案1】:

首先我确保动态控件只会在特定的 div 中创建。然后使用 jquery,我得到了这个 div 中的所有控件,并根据类型编写了代码来处理它们。

这是我的 div,所有控件都将在其中动态创建

<div id="controldiv">
</div>

至于我的 jquery 代码,我创建了每个控件并检查了它的类型

function getval()

  var inputs = $("#controldiv input");
   inputs.each(function () 
     if (this.type == "text")
       //get textbox val here
     
      else  if (this.type == "checkbox")
       // get val here
     
    );

【讨论】:

你如何从这里继续前进? 通过 ajax 将值发送到后端 如果不使用 javascript 或 jquery 和 ajax,则无法在后端获取动态创建的控件的值 你有样品吗? 搜索如何在asp.net中创建webservice以及如何在jquery中做ajax请求。使用 ajax 请求调用创建的 web 服务。

以上是关于使用 JSON 格式的 POST 方法 web api 将动态输入提交到数据库的主要内容,如果未能解决你的问题,请参考以下文章

python requests方法post请求json格式处理

Web传输,前台的参数数据量过大[json格式的字符串],可能达到几M,ajax调用后台方法时

cxf 开发的 WebService,Web项目怎样才能使用 JSON格式访问,并返回JSON格式数据。

用jquery的post方法提交数据需要servlet返回的数据类型是json的数据格式,应该怎么做??

使用axios发送post请求,将JSON数据改为为form类型

如何使用Fiddler2模拟POST和Get提交数据