Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 我的,每个人都不跑,我不知道为什么?_Jquery_Html - Fatal编程技术网

Jquery 我的,每个人都不跑,我不知道为什么?

Jquery 我的,每个人都不跑,我不知道为什么?,jquery,html,Jquery,Html,我试图从API调用数据并将其包含在HTML页面中。数据以JSON格式返回,但是,我试图将其附加到页面上的列表元素中,但它似乎不想合作 function getGames() { $query = "SELECT * FROM games ORDER BY name"; try { global $db; $games = $db->query($query); $games = $games->fetchAll

我试图从API调用数据并将其包含在HTML页面中。数据以JSON格式返回,但是,我试图将其附加到页面上的列表元素中,但它似乎不想合作

    function getGames() {
    $query = "SELECT * FROM games ORDER BY name";

    try {
        global $db;
        $games = $db->query($query);
        $games = $games->fetchAll(PDO::FETCH_ASSOC);
        echo '{"game": ' .json_encode($games) .'}';
    } catch (Exception $ex) {
        echo '{"error": {"text":' .$ex->getMessage() .'}}';
    }
}
上面是我的API调用

以下是我将数据插入页面的方法:

// root URL for restful web services 
var rootURL ="http://localhost/GameReviewWebsite/api/games";
//when thr DOM is ready
$(document).ready(function(){
    findAll();
   // findById();
});

var findAll=function(){
    console.log('findAll');
    $.ajax({
        type: 'GET',
        url: rootURL,
        dataType: 'json', // data type of response
        success: renderList
    });
};

function renderList(data){
    list = data.games;
    $('#gameList li').remove();
    $.each(list, function(index, game){
        $('#gameList').append('<li><a href="#" id="' +game.id+'">'+game.name+'</a></li>');
    });
 }
变量名错误

list = data.games;
应该是

list = data.game;
因为game是Games的名字Ray

     {"game": [
{"id":"2","name":"FIFA 19","developer":"EA Sport","genre":"Sports","review":"2","description":"A game based on real world soccer teams and tournaments."},
{"id":"1","name":"Rocket League","developer":"Psyonix","genre":"Sports","review":"4","description":"A soccer-like game where players driver rocket propelled cars around an arena."},
{"id":"4","name":"SCUM","developer":"Gamepires","genre":"Survival","review":"5","description":"A game where the user plays a human that must survive in a future dominated by robots."},
{"id":"6","name":"Trials Fusions","developer":"RedLynx","genre":"Sports","review":"1","description":"A game where players take control of a skilled motocross rider that tries to navigate through a course filled with obstacles"}
]}
函数renderList(数据){
list=data.game;
$(“#游戏列表li”).remove();
$.each(列表、函数(索引、游戏){
$(“#游戏列表”).append(“
  • ”); }); }

    分配列表变量有输入错误,它应该是
    list=data.game
    ,而不是
    list=data.games

    您确定ajax成功了吗?你能通过添加控制台来检查,并检查成功是否能安慰一些东西吗?是的,它一直运行到$each。我甚至在它前面放了一个模拟附加,它进入了所需列表。快来吧!谢谢现在是02:02,也许是我的眼睛疲劳了!
         {"game": [
    {"id":"2","name":"FIFA 19","developer":"EA Sport","genre":"Sports","review":"2","description":"A game based on real world soccer teams and tournaments."},
    {"id":"1","name":"Rocket League","developer":"Psyonix","genre":"Sports","review":"4","description":"A soccer-like game where players driver rocket propelled cars around an arena."},
    {"id":"4","name":"SCUM","developer":"Gamepires","genre":"Survival","review":"5","description":"A game where the user plays a human that must survive in a future dominated by robots."},
    {"id":"6","name":"Trials Fusions","developer":"RedLynx","genre":"Sports","review":"1","description":"A game where players take control of a skilled motocross rider that tries to navigate through a course filled with obstacles"}
    ]}
    
    function renderList(data){
        list = data.game;
        $('#gameList li').remove();
        $.each(list, function(index, game){
            $('#gameList').append('<li><a href="#" id="' +game.id+'">'+game.name+'</a></li>');
        });
     }