[英]sending javascript array to jsp using ajax
So, I'm working on a MULTIPLE CHOICE QUESTION entry page and i want to handle it completely with ajax. 因此,我正在处理一个“多项选择问题”条目页面,我想用ajax完全处理它。 I want to be flexible with the number of options the question has.
我想对问题的选择数量保持灵活。
Here's the jquery part: 这是jquery部分:
$("#QuestionModPageSubmitButton").click(function(){
var QuesDesc=$("#QuesDesc").val();
var Options=[];
var QuestionId=$("#QuestionId").attr("data-id");
var CorrectOption=$('input[type="radio"]:checked').val();
var TotalOptions=$("#TotalOptions").attr("data-total");
var SubjectId=$("#SubjectId").attr("data-id");
for(var i=0;i<TotalOptions;i++)
{
Options.push($("#Option"+i).val());
}
$.ajax({
type:"POST",
url:"ajax/ModifyQuestion.jsp",
data:{
Subject:SubjectId,
QID:QuestionId,
Question:QuesDesc,
OptionValues:Options,
Correct:CorrectOption,
TotalOptions:TotalOptions},
});
});
I want to sent the Options Array to the jsp page "ModifyQueston.jsp". 我想将选项数组发送到jsp页面“ ModifyQueston.jsp”。
Here's the jsp code i use for reading the sent data: 这是我用于读取已发送数据的jsp代码:
int SubjectId=Integer.parseInt(request.getParameter("Subject"));
int QuestionId=Integer.parseInt(request.getParameter("QID"));
String Question=request.getParameter("Question");
String[] Options=request.getParameterValues("OptionValues");
int CorrectOption=Integer.parseInt(request.getParameter("Correct"));
int TotalOptions=Integer.parseInt(request.getParameter("TotalOptions"));
But with these codes I'm not able to read the array in the jsp page. 但是使用这些代码,我无法读取jsp页面中的数组。 I get NullPointerException when i try to read the length of the Options array or when i try to read values by providing index.
当我尝试读取Options数组的长度或尝试通过提供索引来读取值时,出现NullPointerException。
I guess the script part of sending the data to jsp is fine. 我想将数据发送到jsp的脚本部分很好。 So the question is how to get it into jsp page.
因此,问题是如何将其放入jsp页面。
I tried converting the array into a single string by separating each value with a '-' and then reading it using getParameter() function and then using split() function to separate it back to Array. 我尝试通过用'-'分隔每个值,然后使用getParameter()函数读取它,然后使用split()函数将其分离回Array,将数组转换为单个字符串。
Script: 脚本:
var OptionsString="";
for(var i=0;i<TotalOptions;i++)
{
Options.push($("#Option"+i).val());
OptionsString+=(Options[i]+((i<TotalOptions-1)?" - ":" "));
}
JSP: JSP:
String[] Options=(request.getParameter("OptionValues")).split("-");
It works fine. 工作正常。 But I don't want to do it this way because if any of the options already contains '-' the Code will crash.
但是我不想这样做,因为如果任何选项已经包含“-”,则代码将崩溃。
So, how to get this done? 那么,如何做到这一点呢?
You can sending multiple value by ajax. 您可以通过ajax发送多个值。 From the controller end (for spring framework) you just save it in a string.
从控制器端(对于spring框架),只需将其保存在字符串中即可。 the data will bind with comma separated values.
数据将以逗号分隔的值绑定。 to do that, you need an array from javascript end, i mean your jsp side.
为此,您需要从javascript结束的数组,我的意思是您的jsp方面。
for checkbox you can use: 对于复选框,您可以使用:
var idVal = [];
var i = 0;
$('.case:checked').each(function() {
idVal[i] = $(this).val();
i++;
});
From controller side you can get the value: 从控制器端,您可以获得值:
String[] id = req.getParameter("id_").split(",");
As the same way you can do this for dropdown (options). 同样,您可以对下拉菜单(选项)执行此操作。
This is worked for me when using spring framework. 这在使用spring框架时为我工作。
Thanks. 谢谢。
Okay, so after a couple of weeks of research I found out a way to send the array from js to jsp. 好的,所以经过几周的研究,我找到了一种将数组从js发送到jsp的方法。 The previous code needed just a little modification.
之前的代码仅需进行一些修改。 Here's the js part which needed modification.I just had to add brackets as in arrays, in the data section.
这是需要修改的js部分,我只需要在数据部分的数组中添加括号即可。
$.ajax({
type:"POST",
url:"ajax/ModifyQuestion.jsp",
data:{
Subject:SubjectId,
QID:QuestionId,
Question:QuesDesc,
Correct:CorrectOption,
"Options[]":Options
},
});
Notice that I wrote Options as "Options[]". 注意,我将选项写为“ Options []”。 This makes jsp understand that the variable being sent is an array.
这使jsp理解要发送的变量是一个数组。
Now, the jsp page didn't really require much modification. 现在,jsp页面实际上并不需要太多修改。
String[] Options=request.getParameterValues("Options[]");
And any further operations can be performed as normal strings. 并且任何其他操作都可以作为普通字符串执行。
So, yeah that worked for me!.. 所以,对我有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.