我有Get函数是PHP工作
`// retreive all the movies
function movies_get() {
$this->load->database();
$sql = 'SELECT * FROM movies;';
$query = $this->db->query($sql);
$data = $query->result();
$this->response($data, 200);
}`
http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/movie/movies
在apigee我得到这个结果或这个链接:
`{
"id": "1",
"Title": "Anna Karenina",
"Director": "Joe Wright",
"cast": "Keira Knightley Jude Law Arron Taylor-Johnson",
"genre": "Drama"
},`
我正在尝试使用AJAX来获取要在#movies页面上显示的电影标题。 这是我的代码,但它不起作用,我真的无法弄清楚错误,我是AJAX的新手,但我已经编写了这段代码,错误是什么,可能是我要求显示标题,但它显示的不仅仅是标题?
function Get() {
$.ajax({
url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/movie/movies',
dataType: 'json',
success: function (data) {
$.each(data.movie, function (i, id) {
$('#movies').append('<li> <a href="" data-transition="slide" onclick="Getmovies(' + movies.id + ')">' + movies.Title + '<div class="ui-li-count">' + movies.records + '</div></a><li>');
});
$.mobile.changePage("#movies"); //show the results page
$('#home').listview('refresh');
},
//error: function (response) {
//var r = jQuery.parseJSON(response.responseText);
//alert("Message: " + r.Message);
//}
}
)
};
答案 0 :(得分:1)
AJAX(url:http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/movie/movies)返回的响应本身就是一个数组。因此,您应该迭代'data'而不是'data.movie'。您可以按如下方式更新代码:
$(data.movie).each(function(i,movie){
$('#movies').append('<li> <a href="" data-transition="slide" onclick="Getmovies('+movie.id+')">'+movie.Title+'<div class="ui-li-count">'+movie.records+'</div></a><li>');
});