我正在学习有关对ajax的调用,因此我试图获取('#abcd')(一个html选择)的值。我正在使用以下行:
abcdVal = combo.options [combo.selectedIndex] .value
当此值更改时,我必须将其值存储在类似abcdVal的变量中,以便通过以下方式传递给servlet:
var data = {“ text”:abcdVal};
j("#mybutton").click(function(){
j.ajax({method: 'POST',
url: "/bin/company/repo",
dataType: 'JSON',
data: data,
success:function(result){
alert(result);
j("#demo").html('');
j('#demo').html(result);
}});
});
我得到了值并将响应作为纯文本形式输入,但是在html页面中,我看到了:
[{“ text”:null,“ value”:10}]
代替[{“ text” :( html select的选定值),“ value”:10}]
我做错了什么,然后将数据传递给servlet。我该如何正确通过此变量?
我的密码
JavaScript代码
<script type="text/javascript">
var j = jQuery.noConflict();
var abcdVal;
j(document).ready(function(){
//get a reference to the select element
//request the JSON data and parse into the select element
j.ajax({
url: '/bin/company/repo',
dataType:'JSON',
success:function(data){
//clear the current content of the select
j('#abcd').html('');
//iterate over the data and append a select option
jQuery.each(data, function(text, value){
j('#abcd').append('<option id="' + value.value + '">' + value.text + '</option>');
});
},
error:function(){
//if there is an error append a 'none available' option
j('#abcd').html('<option id="-1">none available</option>');
}
});
j("#abcd").change(function(){
var combo = document.getElementById('abcd');
if(combo.selectedIndex<0)
alert('No hay opcion seleccionada');
else
abcdVal = combo.options[combo.selectedIndex].value;
alert('La opcion seleccionada es: '+combo.options[combo.selectedIndex].value);
});
var data = {"text" : abcdVal};
alert(data);
j("#mybutton").click(function(){
j.ajax({method: 'POST',
url: "/bin/company/repo",
dataType: 'JSON',
data: data,
success:function(result){
alert(result);
j("#demo").html('');
j('#demo').html(result);
}});
});
})
</script>
Servlet代码
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException,
IOException {
String text = (String) request.getParameter("text");
response.setHeader("Content-Type", "text/html; charset=UTF-8");
StringWriter writer = new StringWriter();
TidyJSONWriter json = new TidyJSONWriter(writer);
try
{
json.array();
//loop through your options and create objects as shown below
json.object();
json.key("text");
json.value(text);
json.key("value");
json.value(10);
json.endObject();
//end your array
json.endArray();
} catch(JSONException e) {
e.printStackTrace();
}
response.getWriter().write(writer.toString()); // Write response body.
}
我做了这些更改以解决我的问题:
1)在ajax调用更改方法中:通过类型:“ POST”进行POST。
2)默认情况下,在调用ajax之前添加even.preventDefault(),不要使用提交。
3)更改我处理数据请求的形式。如果我不通过表格,我需要这样做以获取请求参数,例如@Sabya说明。
4)处理成功内的json(ajax)以进行html select的显示选择。
所以代码是下一个:
的JavaScript
<script type="text/javascript">
var j = jQuery.noConflict();
var abcd = document.getElementById("abcd");
var selection = abcd.options[abcd.selectedIndex].value
j(document).ready(function(){
j.ajax({
url: '/bin/company/repo',
dataType:'JSON',
success:function(data){
jQuery.each(data, function(text, value){
j('#abcd').append('<option id="' + value.value + '">' + value.text + '</option>');
});
},
error:function(){
//if there is an error append a 'none available' option
j('#abcd').html('<option id="-1">none available</option>');
}
});
j("#abcd").live('change',function(){
var combo = document.getElementById('abcd');
if(combo.selectedIndex<0)
alert('no option selected');
else
selection = combo.options[combo.selectedIndex].value;
});
j('form').on('submit', function(e){
event.preventDefault();
j.ajax({type: 'POST',
contentType: "application/json; charset=utf-8",
url: "/bin/company/repo",
dataType: 'JSON',
data: JSON.stringify({ "text": selection }),
success:function(data){
jQuery.each(data, function(text, value){
j('#demo').html('');
j('#demo').html(value.text);
});
}});
});
})
</script>
小服务程序
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException,
IOException {
response.setHeader("Content-Type", "application/json");
PrintWriter out = response.getWriter();
StringWriter writer = new StringWriter();
TidyJSONWriter json = new TidyJSONWriter(writer);
StringBuilder buffer = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String data = buffer.toString();
try
{
JSONObject jsonObj = new JSONObject(new String(data));
json.array();
//loop through your options and create objects as shown below
json.object();
json.key("text");
json.value(jsonObj.get("text"));
json.endObject();
//end your array
json.endArray();
} catch(JSONException e) {
e.printStackTrace();
}
out.write(writer.toString());
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句