Ajax 响应作为要绑定到 HTML 的映射对象

Ajax response as Map Object to bind to Html

本文关键字:HTML 映射 对象 绑定 响应 Ajax      更新时间:2023-09-26

我正在将结果数据集作为地图对象。我正在使用下面的脚本将数据附加到 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...}