接收到的json数据如何传递到变量Jsp页面

我正在用jsp创建一个简单的销售系统。我想将数据添加到数据库中。我不知道如何将数据检索到jsp页面中。我如何通过数据功能:

var data = "&items=" + JSON.stringify((items));

$.ajax({
    type: "POST",url: "sales_add.jsp",dataType: 'JSON',data: data,

数据成功传递-我通过控制台检查。

这就是我在jsp页面上检索数据的方式:

String[] json =request.getParameterValues("items"); 

for(int x = 0; x < json.length; x++)
{
    String item = json[x][0]);
    String price = json[x][1]);
    String qty = json[x][2]);
    String $total = json[x][3]);
}

此代码不起作用

所有销售应添加此表数据。我将发送到sales_add.jsp页。我这样检查

console.log(JSON.stringify(items)); 

这是我的完整代码:

function add_product_to_array(item,price,qty,tot)
{
    var item = [item,tot];
    items.push(item);
    console.log(JSON.stringify(items));
}

我通过Console.log检查的表可以像这样成功显示

[[[“巧克力”,32,“ 1”,32]] (索引):237 [[“巧克力”,32,“ 1”,32],[“芒果”,10,“ 1”,10]] 我以这种方式发送** var data =“&items =” + JSON.stringify((items))到sales.add.jsp页面

function addProject()
{
    var data = "&items=" + JSON.stringify((items));
    $.ajax({
        type: "POST",success: function (data) {
            console.log(_data);

            alert("Success");
        },error: function (xhr,status,error) {
            alert(xhr);
            console.log(xhr.responseText);

        }

    });
}

我这样编辑Jsp页面:

String jsonData = request.getParameter("data");

这种格式的先生

[{“ item”:“ Chocolate”,“ price”:“ Chocolate”,“ pro_price”:“ 32”,“ qty”:“ 1”,“ total”:“ 32”},{“ item” :“巧克力”,“价格”:“巧克力”,“ pro_price”:“ 32”,“数量”:“ 1”,“总”:“ 32”},{“商品”:“巧克力”,“价格” :“ Chocolate”,“ pro_price”:“ 32”,“ qty”:“ 1”,“ total”:“ 32”},{“ item”:“ Chocolate”,“ price”:“ Chocolate”,“ pro_price” :“ 32”,“ qty”:“ 1”,“ total”:“ 32”}]

 Connection con1;
PreparedStatement insert;
Class.forName("com.mysql.jdbc.Driver");   
   con1=DriverManager.getconnection("jdbc:mysql://localhost/icepos","root","");        

 for(int x = 0; x < jsonData .length; x++)
        {

              String query = "INSERT INTO sale_product(item,total) VALUES (?,?,?)";

            String   item = jsonData [x]['item']);
            String  price = jsonData [x]['price']);
            String   qty = jsonData [x]['qty']);
            String  total = jsonData [x]['total']);


        }

但是没有这个先生

jdwcjs 回答:接收到的json数据如何传递到变量Jsp页面

//below in HTML part
<input type="hidden" value='<%=request.getAttribute("items") %>' id='items' name='items'>

//below in js part
<script>
let jsonItems=docuement.querySelector('items').value;//now jsonItems item //carry the recived data,using these js function to reFormat the data 
//JSON.stringify() or JSON.parse() based on what you want to do
<script>
本文链接:https://www.f2er.com/3165432.html

大家都在问