gpt4 book ai didi

javascript - (UNtermated String Literal)如何在ajax中打印下拉列表中接收到的json数据

转载 作者:行者123 更新时间:2023-12-03 03:32:35 25 4
gpt4 key购买 nike

我正在尝试为收到的 json 数据放置一个下拉列表,但我遇到了以下错误。请帮助我解决这个问题。我无法找出导致以下错误的原因。它必须打印其中的所有值下拉列表。

值如下所示。

[{"sno":1,"listOfOperators":"Jio"}
{"sno":2,"listOfOperators":"Airtel"},
{"sno":3,"listOfOperators":"Docomo"},
{"sno":4,"listOfOperators":"Ides"},
{"sno":5,"listOfOperators":"Vodacom"},
{"sno":6,"listOfOperators":"Vodafone"}]

语法错误:未终止的字符串文字

请查找以下导致上述错误的代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<title>Operators</title>




<script type="text/javascript">

jQuery(document).ready(function () {
$.ajax({
type: 'POST',
dataType : 'json'',
contentType : "application/json",
url : "${home}getOPList",
cache: false,
success: function(response){
var str = JSON.stringify(response);
var operatorList;
alert("yes");
alert(str);
alert(response);

for (var i = 0; i < response.length; i++ )
{
console.log(response[i].listOfOperators);
operatorList +="<option value = ' "
+response[i].sno+
" ' >"
+ response[i].listOfOperators +
"</option>"

}
$('#opList').html(operatorList);

},
error: function(){
alert('Error while request..');
}
});
});


</script>

</head>
<body>
<div class="logo"></div>
<div class="op-block">
<h1 style="text-align: center;">Please Select an Operator</h1>
<select id="opList">
<option value =' '></option>
</select>

</div>
</body>
</html>

请帮我解决这个问题

最佳答案

确保您的响应有效后,您应该循环遍历它:

    var response = [
{"sno":1,"listOfOperators":"Jio"},
{"sno":2,"listOfOperators":"Airtel"},
{"sno":3,"listOfOperators":"Docomo"},
{"sno":4,"listOfOperators":"Ides"},
{"sno":5,"listOfOperators":"Vodacom"},
{"sno":6,"listOfOperators":"Vodafone"}
];

for (var i = 0; i < response.length; i++ )
{
console.log(response[i]);
}

关于javascript - (UNtermated String Literal)如何在ajax中打印下拉列表中接收到的json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46022552/

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