gpt4 book ai didi

javascript - Ajax 响应作为映射对象绑定(bind)到 Html

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:26 25 4
gpt4 key购买 nike

我将结果数据集作为 map 对象获取。我正在使用下面的脚本将数据附加到 Html。但并没有影响。

$("#district").change(
function() {
$('#mandal').html('');
var district = {
"district" : $("#district").val()
};
$.ajax({
url : "Reports",
data : JSON.stringify(district),
dataType : 'json',
contentType : 'application/json',
type : 'POST',
async : true,
success : function(res) {
console.log(res.resList.length);
for ( var i = 0; i < res.resList.length; i++) {
console.log("Kalishavali " + res.resList[i]);
$('#mandal').append(
'<option value=' + res.resList[i] + '>'
+ res.resList[i]
+ '</option>');
}
}
});
});

结果数据格式:

{08=Prakasam, 09=S.P.S Nellore, 04=East Godavari, 05=West Godavari, 06=Krishna, 07=Guntur, 13=Kurnool, 01=Srikakulam, 11=Kadapa, 02=Vizianagaram, 12=Anantapur, 03=Visakhapatnam, 10=Chittoor}

这是我的 Jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@include file="includes/Header_1.html" %>
<script>
$(function() {
$("#district").change(
function() {
$('#mandal').html('');
var district = {
"district" : $("#district").val()
};
$.ajax({
url : "Reports",
data : JSON.stringify(district),
dataType : 'json',
contentType : 'application/json',
type : 'POST',
async : true,
success : function(res) {
console.log(res.resList.length);
for ( var i = 0; i < res.resList.length; i++) {
console.log("Kalishavali " + res.resList[i]);
$('#mandal').append(
'<option value=' + res.resList[i] + '>'
+ res.resList[i]
+ '</option>');
}
}
});
});
});
</script>


<s:select label="District" list="resList" listKey="key" value="value" name="district" headerKey="-1" headerValue="Select District"/>
Mandal :
<select id="mandal"></select>

最佳答案

响应不是有效的 JSON:

  1. 键值对之间用 = 分隔,而不是 :
  2. 周围没有引号

应该是:

{"08" : "Prakasam", "09" : "S.P.S Nellore" etc...}

关于javascript - Ajax 响应作为映射对象绑定(bind)到 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com