gpt4 book ai didi

javascript - JSON相关错误: Unexpected token o

转载 作者:行者123 更新时间:2023-11-28 13:28:46 25 4
gpt4 key购买 nike

我的数据库表:

TABLE `events` (
`event_id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
`event_title` VARCHAR(255) NOT NULL,
`event_desc` TEXT,
`event_location` VARCHAR(255) NOT NULL,
`event_requirements` TEXT DEFAULT NULL,
`event_date` DATETIME NOT NULL,
PRIMARY KEY (`event_id`)

目标:当在选择下拉菜单中选择一个事件时,我想显示其余的数据库数据,这些数据属于下面 div 中的事件。

尝试解决问题(在 MVC 架构中):

Controller

  function index()
{
$overview_model = $this->loadModel('Events');
$this->view->events = $overview_model->getEventTypes();
$this->view->render('events/index');
}

型号

  public function getEventTypes()
{
$sth = $this->db->prepare("SELECT * FROM events");
$sth->execute();

$events = array();

foreach ($sth->fetchAll() as $event) {
$events[$event->event_id] = new stdClass();
$events[$event->event_id]->event_id = $event->event_id;
$events[$event->event_id]->event_title = $event->event_title;
$events[$event->event_id]->event_desc = $event->event_desc;
$events[$event->event_id]->event_location = $event->event_location;
$events[$event->event_id]->event_requirements = $event->event_requirements;
$events[$event->event_id]->event_date = $event->event_date;
}

return $events;
}

查看

    <!-- start event type selection --> 
<div class="event-selection">
<label>Choose an event:</label>
<select id="select-event-type">
<?php foreach ($this->events as $event) {
// Using the event's ID as a value makes it easier for us to look up the selected event.
echo "<option value='" .$event->event_id. "'>" .$event->event_title."</option>";
}?>
</select>

<script>
(function ($) {
// Here we set the JavaScript variable 'events' to a JSON-encoded copy of the array we passed to the template, or null (if there are no events).
var events = <?php echo (count($this->events) > 0) ? "JSON.parse(" .json_encode($this->events). ");" : "null"; ?>
$(document).ready(function() {
$('#select-event-type').change(function() {
if (events) {
var event = events[this.value];
$('#event-details').html(event.title);
}
});
});
})($);
</script>

<!-- Start event-details -->
<div id="event-details"></div> <!-- /#event-details -->

</div><!-- /.event-selection -->

当我更改下拉选择时,页面上没有任何反应。

控制台输出(在 Chrome 中):

        <script>
(function ($) {
var events = JSON.parse({"1":{"event_id":"1","event_title":"event1","event_desc":"event1","event_location":"eventlocation","event_requirements":"event1","event_date":"2022-07-20 15:00:00"},"2":{"event_id":"2","event_title":"event2","event_desc":"event2","event_location":"eventlocation","event_requirements":"event2","event_date":"2015-04-20 15:00:00"},"3":{"event_id":"3","event_title":"event3","event_desc":"event3","event_location":"eventlocation","event_requirements":"event3","event_date":"2019-11-20 16:00:00"}}); $(document).ready(function() {
$('#select-event-type').change(function() {
if (events) {
// Now we can get the event details from the selected value.
var event = events[this.value];
$('#event-details').html(event.title);
}
});
});
})($);
</script>

错误:

Uncaught SyntaxError: Unexpected token o

根据我的研究,产生错误的是这个(在 View 中):

var events = <?php echo (count($this->events) > 0) ? "JSON.parse(" .json_encode($this->events). ");" : "null"; ?>

控制台中的 JSON 格式是否正确?另一个线程建议使用 $.getJSON() 而不是 JSON.parse(),但是,由于这是我第一次使用 JSON,我不太确定这在我的情况下会如何工作。
我非常感谢任何形式的帮助!

最佳答案

var events = JSON.parse({"1":{"…

您正在尝试解析 JavaScript 对象文字,就好像它是 JSON 一样。

由于您必须有一个字符串才能解析 JSON,因此它会被转换为相当于以下内容的字符串:

var events = JSON.parse("[object Object]");

那不是 JSON。

直接使用对象文字:

var events = {"1":{"…

关于javascript - JSON相关错误: Unexpected token o,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782610/

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