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

时间:2017-09-03 10:35:50

标签: javascript jquery json ajax drop-down-menu

我正在尝试为收到的json数据添加一个下拉列表但是我得到的错误。请帮我解决这个问题。我无法弄清楚是什么导致下面的erroe.it必须打印所有的值下拉列表。

,值如下所示。

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

SyntaxError:unterminated string literal

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

<%@ 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>

请帮我这个

1 个答案:

答案 0 :(得分:1)

确保您的response有效后,您应该循环播放它:

&#13;
&#13;
    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]);
    }
&#13;
&#13;
&#13;