我试图加载JSON数据与AJAX,但它不起作用。每次ajax调用错误函数并且不调用成功函数。加载JSON数据与AJAX和PHP
我的AJAX调用:
$(document).on("click", "#myMovies .btn-update", function() {
var id = $(this).parent().data("id");
$.ajax({
url : 'index.php',
type : 'POST',
dataType: 'json',
data : 'id=' + id + '&action=update',
success : function(data){
$('#updateMovie')
.find('[name="title"]').val(data.title).end()
.find('[name="list"]').val(data.list).end();
},
error : function(jqXHR, textStatus, errorThrown){
console.log("error");
alert(textStatus);
alert(errorThrown);
}
});
});
的index.php的interessing部分:
else if($_POST['action'] == "update") {
/*getSpecificMovie($_POST['id']);
$movies = getSpecificMovie();
$results = Utils::secureMoviesJSON($movies);
echo $results;*/
header("Content-Type: application/json", true);
$array = array(
'title' => 'test',
'list' => 'test');
echo json_encode($array, JSON_FORCE_OBJECT);
}
任何人都知道是我的错? 谢谢你的回答。
是否写 “错误”,在控制台? console.log(“error”,jqXHR,textStatus,errorThrown);在错误回调中打印? – lisztomania
不确定,但你可以尝试用'data:{'id':id','action':'update'}替换'data:'id ='+ id +'&action = update','' –
是的控制台显示错误然后警报(textStatus)显示:“parsererror”和警报(errorThrown)显示:“synthaxError:意外的令牌<” – onedkr