gpt4 book ai didi

php - 索引.php :13 Uncaught SyntaxError: Unexpected token: error message in Ci

转载 作者:行者123 更新时间:2023-11-29 06:12:00 26 4
gpt4 key购买 nike

在我的 Javascript 代码中,在以下行之后:

url: ....../Misc-2/Ci-TodoList/index.php/home/jsonAddData,

我收到以下错误:

index.php:13 Uncaught SyntaxError: Unexpected token :

<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#add').bind('keypress', function(e) {
if(e.keyCode == 13){
$.ajax({
type: "POST",
dataType: "JSON",
url: <?php echo site_url("home/jsonAddData"); ?>,
data: dataString,
json: {title_posted: true},
success: function(data){
if(data.title_posted == true) { // true means data was successfully posted.
$("#success").append("Success");
} else if(data.title_posted == false) { // false means data failed to post.
$("#success").append('Failure');
}
}
});
}
});

}
});
});
</script>

经过一些调试技巧后,我非常肯定 index.php:13 指的是我的 PHP 脚本。它所做的一件事是,当我加载 url 帮助程序时,它说这是导致错误的原因。然后我自动加载了 url 帮助程序,现在它说第 13 行导致了错误,但第 13 行只是一个 mysql 选择查询,您可以在下面确定:

<?php
class home extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data = array();
$data['lists'] = $this->displayList();
$this->load->view('home', $data);
}
function displayList() {
$str = '';
$query = $this->db->query("SELECT * FROM data");
foreach ($query->result() as $row) {
$b = '<input name="completed" type="checkbox" />';
$a = $row->title . "<br>";
$str .= $b.$a;
}
return $str;
}
function jsonAddData() {
if($this->input->is_ajax_request()) {
header('Content-type:application/json');
$title = $this->input->post('title');
$query = $this->db->query("INSERT INTO data (title) VALUES ('$title')");
if($query) return json_encode(array('title_posted' => true));
else return json_encode(array('title_posted' => false));
}
}
}
?>

对于为什么会发生这种情况有什么想法吗?

最佳答案

您收到 JS 错误,因此 PHP 语法不是罪魁祸首。 :)

换行<?php echo site_url("home/jsonAddData"); ?>用双引号引起来(我的意思是 "" )。此外,您还有其他语法错误,正确的缩进可以节省您将来的时间。

这是已解决的版本:

$(document).ready(function() {
$('#add').bind('keypress', function(e) {
if(e.keyCode == 13){
$.ajax({
type: "POST",
dataType: "JSON",
url: "<?php echo site_url("home/jsonAddData"); ?>",
data: dataString,
json: {title_posted: true},
success: function(data){
if(data.title_posted == true) { // true means data was successfully posted.
$("#success").append("Success");
} else if(data.title_posted == false) { // false means data failed to post.
$("#success").append('Failure');
}
}
});
}
});
});

关于php - 索引.php :13 Uncaught SyntaxError: Unexpected token: error message in Ci,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8646086/

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