首页 > 解决方案 > 使用 Spring Boot 将 JavaScript 对象的请求发布到 JSON

问题描述

我是编码新手,我正在使用带有 POST 请求的 Spring Boot 的 Crud Repository 保存单行数据,//但我想知道如何使用 Spring Boot 在单个 POST 请求中保存多行数据:

以下是我发送单行数据的过程:

// Declaring  Variable for MySQL Table Column  for getting html data into JavaScript



 var data = {
           billNo : "",
     descriptions : "",
          account : "",
          amount  : 0,
    };

// Setting HTML Values into JavaScript
// I Used 'x' for multiple Rows for HTML document ID, 'x' is 0 for First Row, 1 for 2nd Row and go on.
            
data.billNo = document.getElementById("BillNo"+x).value;
data.descriptions = document.getElementById("Description"+x).value;
data.acount = document.getElementById("Account"+x).value;
data.amount = document.getElementById("Amount"+x).value;

// Converting JavaScript to JSON

var json = JSON.stringify(data);
    
//Sending POST Request : Every thing is OK if Single Row Data
HTTP('Post','/expenses',json).then(jsonData => {
      //Start of Request Object
        console.log(jsonData);
        //End of Request Object

});

//HTTP Request Function

const HTTP = (method, url,JSON)=>{
    const PROMISE = new Promise((resolve, reject) =>{
        const XHRR = new XMLHttpRequest();
        XHRR.open(method,url);
        XHRR.responseType="json";
        XHRR.setRequestHeader('Content-Type', 'application/json');
        XHRR.onload = () => {
            resolve(XHRR.response);
            };
        XHRR.send(JSON);
    });
    
    return PROMISE;
};

我可以使用 HTML 动态行,使用动态文档 ID。图片附在我的 HTML 屏幕上

我想知道如何为多行保存数据

标签: javascriptjsonspring-bootrest

解决方案


推荐阅读