Javascript $.parseJSON意外字符
我试图从Javascript $.parseJSON意外字符,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我试图从span元素上的htmldata属性发送数据,并使用Ajax接收数据,然后使用php和mysql处理数据,并将新值返回到html中的数据属性,但我收到一个错误,错误是“$.parseJSON意外字符”,有没有人能看一下我的代码,看看我是否正确地处理了数据,因为我刚刚开始使用JSON HTML/PHP <span data-object= '{"art_id":"<?php echo $row['art_id'];?>", "art_featured":"<?php
span
元素上的htmldata
属性发送数据,并使用Ajax接收数据,然后使用php和mysql处理数据,并将新值返回到html中的数据属性,但我收到一个错误,错误是“$.parseJSON意外字符”,有没有人能看一下我的代码,看看我是否正确地处理了数据,因为我刚刚开始使用JSON
HTML/PHP
<span data-object=
'{"art_id":"<?php echo $row['art_id'];?>",
"art_featured":"<?php echo $row['art_featured'];?>"}'
class="icon-small star-color"></span>
<!-- art_id and art_featured are both int and art_featured will be either 1 or 0 -->
PHP/mySQL
if($_POST['art_featured']==1) {
$sql_articles = "UPDATE `app_articles` SET `art_featured` = 0 WHERE `art_id` =".$_POST['art_id'];
$result = array('art_id' => $_POST['art_id'], 'art_featured' => 0);
echo json_encode($result);
}
else if($_POST['art_featured']==0){
$sql_articles = "UPDATE `app_articles` SET `art_featured` = 1 WHERE `art_id` =".$_POST['art_id'];
$result = array('art_id' => $_POST['art_id'], 'art_featured' => 1);
echo json_encode($result);
}
if(query($sql_articles)) {
}
else {
}
您不需要使用
$.parseJSON
,jQuery会为您这样做
$("span[class*='star']").on('click', function () {
var data = $(this).data('object');
var $this = $(this);
$.ajax({
type: "POST",
url : "ajax-feature.php",
data: {art_id: data.art_id,art_featured: data.art_featured}
}).done(function(result) {
data.art_featured = result;
$this.data('object', data);
});
});
您以后也不需要对其进行字符串化。$sql\u articles=“UPDATE app\u articles SET art\u featured=0,其中art\u id=“.$\u POST['art\u id'”代码>您需要修复此查询(以及第二个查询),它们都对SQL注入非常开放。谢谢,是的,我还没有完成SQL页面,我只是在保护它之前让它工作,但是谢谢!
$("span[class*='star']").on('click', function () {
var data = $(this).data('object');
var $this = $(this);
$.ajax({
type: "POST",
url : "ajax-feature.php",
data: {art_id: data.art_id,art_featured: data.art_featured}
}).done(function(result) {
data.art_featured = result;
$this.data('object', data);
});
});