发送2d数组jquery帖子到Java servlet [英] sending a 2d array jquery post to java servlet

查看:122
本文介绍了发送2d数组jquery帖子到Java servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送一些二维数组中的数据.我发现您通过此处发送一个数组 此处,但我需要从中发送2D数组我的JavaScript使用发布到Java Servlet的方法.有办法吗?

I have some data that I need to send that is in a 2D array. I found that you send an array through post here here but i need to send a 2D array from my javascript using post to a java servlet. Is there a way to do this?

推荐答案

您可以使用与链接到的示例完全相同的技术.这是因为它使用JSON序列化数据,因此可以一次性发送整个JS数据结构.因此,以该示例为例,但将其重建为2d数组并对其进行调整以发送实际的JSON:

You can use exactly the same technique as the example you link to. This is because it uses JSON to serialise the data, so it can send entire JS data structures in one go. So, taking the example, but rebuilding it for a 2d array and tweaking it to send actual JSON:

var obj=[ [1.1, 1.2], [2.1, 2.2], [3.1, 3.2] ];
$.ajax({
        url:"myUrl",
        type:"POST",
        dataType:'json',
        success:function(data){
            // codes....
        },
        data:JSON.stringify(obj),
        contentType: 'application/json'
    });

然后,它将向您的服务器发送一个字符串,例如:

Then this will send a string to your server, like:

"[[1.1,1.2],[2.1,2.2],[3.1,3.2]]"

然后,您的Java servlet将不得不反序列化该代码,然后根据需要使用它.在此示例中,JSON将作为RAW帖子数据发送;如果要通过请求对象获取它,可以执行以下操作:

Your Java servlet will then have to deserialise this and use it however you want. In this example, the JSON will be sent as RAW post data; if you want to get it via the request object, you can do something like:

var obj=[ [1.1, 1.2], [2.1, 2.2], [3.1, 3.2] ];
$.ajax({
        url:"myUrl",
        type:"POST",
        dataType:'json',
        success:function(data){
            // codes....
        },
        data: {json: JSON.stringify(obj)}
    });

那么您应该能够从以下位置获取JSON字符串:

Then you should be able to get the JSON string from:

request.getParameterValues("json");

这篇关于发送2d数组jquery帖子到Java servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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