我試圖加載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