AJAX post in using slim framework outputs 500 (Internal Server Error)

萝らか妹 提交于 2019-12-11 19:48:10

问题


I have been stuck on this for days now, here is my previous question, but none of the comments seem to work. I have tried multiple things and managed to get rid of the 404 error, but now there is a 500 error...

  • Basically I want to post an AJAX query when an event happens.
  • Run a Slim post route using the AJAX query parsing through a value.
  • Select data from the database using the Slim post route using the parsed through value.
  • Then return the results from the query and display them in a selection box.

If anyone has any idea how to solve this I would be so grateful!!!

Here is the code:

//Post the AJAX request
<script>
    $(document).ready(function() {
        $('select#add-module-stackid').change(function() {
            var val = document.getElementById("add-module-stackid");
            var val2 = val.options[val.selectedIndex].value;
            //alert(val2);
            $.ajax({ 
                url: '/admin-get-add-module',
                data:{stackid:val2},
                dataType:'json',
                type: 'POST',
                success: function(response) {
                    console.error(JSON.stringify(response));
                    alert(response);
                },
                error: function() {
                    console.error("error");
                    alert('Not working!');
                }
            });
        });
    });
</script>

Here is the Slim Post Route code:

$app->post('/admin-get-add-module', function () use($app){
$req = $app->request();
$myStacks = filterStacks($app);
$app->view()->appendData(array('data' => $data, "stacks"=> $myStacks, "username" => getUsername($app)));
$sql = $link->prepare("select * from evironments where StackID=?");
$sql->bind_param("i", $stackid);
})->name('admin-get-add-module');

It doesn't seem to run the route

Image to show console error:

Here is the header error:


回答1:


I got rid of the 500 internet server error. I wasn't parsing through the link.

$app->post('/admin-get-add-module', function () use($app,$link){ <<<<<<<<LINK HERE
    $req = $app->request();
    $stackid = $_POST['stackid'];
    $myStacks = filterStacks($app);
    $app->view()->appendData(array("username" => getUsername($app)));
    $sql = $link->prepare("select * from evironments where StackID=?");
    error_log(var_export($sql,true));
    $sql->bind_param("i", $stackid);
})->name('admin-get-add-module');

It still doesn't work though here is a link to the sql / ajax problem



来源:https://stackoverflow.com/questions/18828888/ajax-post-in-using-slim-framework-outputs-500-internal-server-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!