Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Json 使用SlimAPI的Web应用程序。I';我试图从我的SQL数据库中获取数据,以便在表中单击编辑链接时填充模式_Json_Ajax_Rest_Bootstrap Modal_Slim - Fatal编程技术网

Json 使用SlimAPI的Web应用程序。I';我试图从我的SQL数据库中获取数据,以便在表中单击编辑链接时填充模式

Json 使用SlimAPI的Web应用程序。I';我试图从我的SQL数据库中获取数据,以便在表中单击编辑链接时填充模式,json,ajax,rest,bootstrap-modal,slim,Json,Ajax,Rest,Bootstrap Modal,Slim,调试时;单击“编辑”按钮时会找到每一行的id,但此时它似乎卡住了。所述行的数据不会填充模式。如果有任何帮助,我将不胜感激。我已经用尽了我所有的想法。我确实有一个错误,因为localhost未能加载图像文件夹的资源,这可能会导致冲突吗?我不知道怎么做 HTML 您的服务器端PHP方法正在输出无效JSON的数据 除去 echo $query; 从您的代码中,因为它只是调试的剩余部分。这使得jQuery无法将整个响应视为JSON并进行相应的解析。您所说的“在这里卡住”(代码中的注释)是什么意思??“

调试时;单击“编辑”按钮时会找到每一行的id,但此时它似乎卡住了。所述行的数据不会填充模式。如果有任何帮助,我将不胜感激。我已经用尽了我所有的想法。我确实有一个错误,因为localhost未能加载图像文件夹的资源,这可能会导致冲突吗?我不知道怎么做

HTML


您的服务器端PHP方法正在输出无效JSON的数据

除去

echo $query;

从您的代码中,因为它只是调试的剩余部分。这使得jQuery无法将整个响应视为JSON并进行相应的解析。

您所说的“在这里卡住”(代码中的注释)是什么意思??“成功”功能是否执行?控制台中有记录吗?这是你期望的吗?如果没有,那么会发生什么?ajax请求完成了吗?它是否返回OK(200)状态?回答是什么?同样,如果不是,会发生什么?“被卡住”或“无法填充”的调试信息确实不足以帮助解决问题。这里有很多代码,所以我们需要找出问题所在,而且我们也看不到您的服务器方法或任何可能起作用的数据。对于缺乏明确信息,我们深表歉意。“成功”功能不执行。“id”登录到控制台,并返回OK(200)状态和数据的URL;“”。如果它返回200 OK,但是“success”没有运行,那就没有任何意义,除非您得到一个控制台错误,响应数据无法解析?正如我所说,对该调用的响应是什么(查看ajax请求的内部,在您的网络选项卡中)?它是有效的JSON吗?您期望的是什么?如果ajax调用运行并返回200 OK,那么“成功”将始终执行,除非返回的数据不能解释为有效的JSON。好的,是的,返回有效的JSON。您能给我看一下数据吗?因为函数不起任何作用,这看起来很奇怪。
        var rootURL ="http://localhost:4006/GamesAPI/api/games";
        var currentGame;
        //when the DOM is ready
        $(document).ready(function(){
            findAll();
            //findById();
            $(document).on("click","#admin_table_body a",function(){findById(this.id);});
           // $(document).on("click","#addButton",function(){addGame();});
           // $(document).on("click","#deleteButton",function(){deleteGame();});

        });

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

        var findById = function(id)
        {
            console.log('findById: '+id);
            $.ajax({
                type: 'GET',
                url: rootURL + '/' + id,
                dataType: "json",
                //Gets stuck here
                success: function(data){
                    //$('#btnDelete').show();
                    console.log('findById success: ' +data.title);
                    currentGame = data;
                    renderDetails(currentGame);

                }


            });
        };


        function renderList(data){
            list = data.games;
            console.log("renderList");
            $('#admin_table_body tr').remove();
            $.each(list, function(index, games){
                $('#admin_table_body').append('<tr><td>' +games.title+'</td><td>'+games.genre+'</td><td>' 
                        +games.platform+'</td><td>' +games.score_phrase+'</td><td>'
                        +games.score+'</td><td>'+games.release_year+'</td><td>'+games.release_month+'</td><td>'
                        +games.release_day+'</td><td>'+games.editors_choice+'</td><td>\n\
                        <a href="#"  id="'+games.id+'" "span class="fa fa-pencil" data-toggle="modal" data-target="#editModal">Edit</a></td>\n\
                        <td id="'+games.id+'"><button type="button" id="deleteButton" class="btn btn-success">Delete</button></td></tr>');
            });
            $('#admin_table').DataTable();


           // $('gameList').append('<div class="row">');

           //The rest of this function is to populate a different client page
           output='<div class="row">';
            $.each(list, function(index,games){
                var img="pics/"+games.picture;
                output+=('<div class="col-sm-6 col-md-4 col-lg-3"><div class="card"><img src='+'"'+img+'"'+
                        'height="150"><p>Title: '+games.title+'</p><p>Genre: '+games.genre+'</p><p>Platform: '+games.platform+
                        '</p><p>Score: '+games.score+'  '+games.score_phrase+'</p></div></div>');
              // $('#gameList').append('<div class="col-sm-6 col-md-4 col-lg-3"><div class="card">'+game.title+'</div></div>');
           }); 
         // $('#gameList').append('</div>');
           output+='</div>';
           $('#productList').append(output);
            }; 


        var renderDetails = function(games)
        {
            $('#id').val(games.id);
            $('#title').val(games.title);
            $('#url').val(games.url);
            $('#platform').val(games.platform);
            $('#score').val(games.score);
            $('#score_phrase').val(games.score_phrase);
            $('#genre').val(games.genre);
            $('#pic').attr('src', 'pics/' + games.picture);
            $('#editors_choice').val(games.editors_choice);
            $('#release_year').val(games.release_year);
            $('#release_month').val(games.release_month);
            $('#release_day').val(games.release_day);

        };
function getGame($id) { 
    $query = "SELECT * FROM games WHERE id = '$id'";
    try {
        global $db;
        $games = $db->query($query);  
        $game = $games->fetch(PDO::FETCH_ASSOC);
                header("Content-Type: application/json", true);
        echo $query;
                echo json_encode($game);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}
echo $query;