查看: 303|回复: 0

[文章教程] JS 提交表单JSON数据格式的两种方法

[复制链接]
xuanxiao 发表于 2023-5-10 11:40:38 | 显示全部楼层 |阅读模式
方法1:
  1. $(document).ready(function(){

  2.     $("#submit").on('click', function(){

  3.    var formData = {

  4.                                 "dAddTS": $('input[name=dAddTS]').val(),
  5.                                 "sName": $('input[name=sName]').val(),
  6.                                 "shaoma": $('input[name=shaoma]').val(),
  7.                                 "sgjc": $('input[name=sgjc]').val(),
  8.                                 "iAdmin_Id": $('input[name=iAdmin_Id]').val(),
  9.                                 "iRole": $('input[name=iRole]').val(),
  10.                                 "dcl": $('input[name=dcl]').val(),
  11.                                 "iAdmin_CS": $('input[name=iAdmin_CS]').val(),
  12.                                 "iAdmin_zj": $('input[name=iAdmin_zj]').val(),
  13.                                 "tgzh": $('input[name=tgzh]').val()
  14.     };

  15.        $.ajax({
  16.         headers: {
  17.             'Accept': 'application/json',
  18.             'Content-Type': 'application/json'
  19.         },
  20.         url: '/345345345/xierudaima.asp',
  21.         type : "POST",
  22.         dataType : 'json',
  23.         data : JSON.stringify(formData),
  24.         success : function(result) {

  25.             console.log(result);
  26.         },
  27.         error: function(xhr, resp, text) {
  28.             console.log(xhr, resp, text);
  29.         }
  30.     })
  31. });
  32. });
复制代码


方法2:

  1. <script type="text/javascript">
  2.   $('register_form').on('submit', function(event){

  3.     var obj = $('register_form').serializeJSON();

  4.     $.ajax({
  5.         type: 'POST',
  6.         url: 'https://url.com/users/register',
  7.         dataType: 'json',
  8.         data: JSON.stringify(obj),
  9.         contentType : 'application/json',
  10.         success: function(data) {
  11.             alert(data)
  12.         }
  13.     });

  14.    return false;
  15. });
  16. </script>
复制代码


提交数据结果:
微信截图_20230510114011.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号