【发布时间】:2015-10-05 14:25:23
【问题描述】:
我正在尝试使用 ajax 加载 json 数据,但它不起作用。每次ajax调用错误函数并且不调用成功函数。
我的 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 中有趣的部分:
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);在错误回调中打印?
-
不确定,但您可以尝试将
data : 'id=' + id + '&action=update',替换为data : {'id': id, 'action' : 'update'}, -
是的,控制台显示错误,然后 alert(textStatus) 显示:"parsererror" 和 alert(errorThrown) 显示:"synthaxError : Unexpected token
-
John C:不,它仍然不起作用:(
标签: javascript php jquery json ajax