简体   繁体   English

承诺返回得到500个内部服务器错误

[英]Promise returning get 500 internal server error

I made a PHP function that echo's JSON after a form is submitted and I want to interact with that with a promise request. 我制作了一个PHP函数,该函数在提交表单后回显JSON,我想与一个Promise请求进行交互。 I keep getting a 500 internal server error and I don't know why. 我不断收到500个内部服务器错误,但我不知道为什么。

Here are my files - this is registerFail.js that's supposed to get the JSON after the form is submitted so I can alert a message, it's in the main directory : 这是我的文件-这是registerFail.js,应该在提交表单后获取JSON,以便我可以提醒消息,它位于主目录中:

window.onload = function(){

    function get(url) {
        return new Promise((resolve, reject) => {
          const xhr = new XMLHttpRequest();
          xhr.open("GET", url);
          xhr.onload = () => resolve(xhr.responseText);
          xhr.onerror = () => reject(xhr.statusText);
          xhr.send();
        });
      }

    document.getElementById('register').addEventListener('submit', () => {
            let promise = get('/controllers/add-user.php').then((name) => {
            console.log(name);
        });
    });
}

This is my controller php class in the directory /controllers/add-user.php, and it is where the promise in registerFail.js is supposed to get the json from 这是我的控制器php类,位于目录/controllers/add-user.php中,这是registerFail.js中的promise应该从中获取json的地方

header('Content-type: text/javascript');

$hash = password_hash($_POST['password'], PASSWORD_BCRYPT);

if(!$app['database']->nameTaken($_POST['username'])){

    $app['database']->insert('users', [

        'name' => $_POST['username'],

        'password' => $hash

    ]);
};

header('Location: /'); 

the ->nameTaken function is what echo's the JSON. -> nameTaken函数是回显的JSON。

Here are the nameTaken function that's in a different directory and file - 这是位于不同目录和文件中的nameTaken函数-

public function nameTaken($username){

        $statement = $this->pdo->prepare('SELECT count(*) FROM users WHERE name = :name');

        $statement->execute(array('name' => $username));

        $res = $statement->fetch(PDO::FETCH_NUM);

        $exists = array_pop($res);

        if ($exists > 0) {

            $json = array(
                'success' => false
            );

            echo json_encode($json);

            return true;

        } else {
            //the name can be made
            return false;
        }
    }

In my server I'm getting these messages : 在我的服务器上,我收到以下消息:

[Thu Aug 16 12:16:18 2018] 127.0.0.1:44906 [200]: /
[Thu Aug 16 12:16:18 2018] 127.0.0.1:44910 [200]: /registerFail.js
[Thu Aug 16 12:16:22 2018] PHP Notice:  Undefined index: password in /home/orpheus/Practice_dev/imagePoster/controllers/add-user.php on line 5
[Thu Aug 16 12:16:22 2018] PHP Notice:  Undefined variable: app in /home/orpheus/Practice_dev/imagePoster/controllers/add-user.php on line 7
[Thu Aug 16 12:16:22 2018] PHP Fatal error:  Uncaught Error: Call to a member function nameTaken() on null in /home/orpheus/Practice_dev/imagePoster/controllers/add-user.php:7
Stack trace:
#0 {main}
  thrown in /home/orpheus/Practice_dev/imagePoster/controllers/add-user.php on line 7
[Thu Aug 16 12:16:22 2018] 127.0.0.1:44996 [500]: /controllers/add-user.php - Uncaught Error: Call to a member function nameTaken() on null in /home/orpheus/Practice_dev/imagePoster/controllers/add-user.php:7
Stack trace:
#0 {main}
  thrown in /home/orpheus/Practice_dev/imagePoster/controllers/add-user.php on line 7
 [Thu Aug 16 12:16:22 2018] 127.0.0.1:45000 [200]: /users

It looks like my promise is executing and trying to get the json before the php file has loaded so none of the json is there and I'm getting these errors for undefine variables. 看来我的诺言正在执行并尝试在php文件加载之前获取json,因此没有json,并且我收到这些错误的未定义变量。 I see the 500 error for a split second before the /users ( /controllers/add-user.php) php json file is loaded and see all those errors above in my server, but then when the php file has loaded the post from the form, it all works properly and I see the correct json, but when I'm redirected back to the '/' main file, the console is cleared and my promise does not console.log the json. 我在加载/ users(/controllers/add-user.php)php json文件之前一瞬间看到了500错误,并在服务器中看到了上面所有的错误,但是当php文件从表单,它们都可以正常工作,并且我可以看到正确的json,但是当我重定向回“ /”主文件时,将清除控制台,而我的承诺也不会console.log json。

So I think my problem is that the promise executes immediately after my the submit button is clicked on, but I need it to execute after the php json file has loaded so I don't get the 500 server error in my console and the server errors from my terminal shown above. 所以我认为我的问题是,在单击提交按钮后,promise会立即执行,但是我需要在php json文件加载后执行,所以我不会在控制台中看到500服务器错误,也不会出现服务器错误从上面显示的我的终端。

How can I get my promise to execute after the php file has loaded and I'm redirected back to the '/' page? php文件加载后,如何重定向到“ /”页面,我如何才能履行承诺?

I just discovered sesssions and using them worked. 我只是发现了烦恼,并使用了它们。 Here is my code in add-user.php 这是我在add-user.php中的代码

<?php

session_start();

$hash = password_hash($_POST['password'], PASSWORD_BCRYPT);

if(!$app['database']->nameTaken($_POST['username'])){

    $app['database']->insert('users', [

        'name' => $_POST['username'],

        'password' => $hash

    ]);
} else {
    $_SESSION['error'] = 'Username is taken';
}

header("location:/");

Then I made the session equal to a variable in my controller class for the page with my form 然后,使会话等于表单上页面的控制器类中的变量

session_start();

$error = $_SESSION['error'];

and then I made an if statement in my form page to show the $error variable if its set, then unset the session and its working how I want it to. 然后,我在表单页面中创建了一个if语句,以显示$ error变量(如果已设置),然后取消会话设置以及其工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获(承诺)500(内部服务器错误)BadMethodCallException - Uncaught (in promise) 500 (Internal Server Error) BadMethodCallException 获取返回 500(内部服务器错误) JavaScript - Fetch returning 500(internal server error) JavaScript Ajax调用返回500内部服务器错误 - Ajax call returning 500 internal server error 返回JsonResult会导致500内部服务器错误 - Returning JsonResult results in 500 Internal Server Error React 应用程序返回 500 内部服务器错误 - React app returning 500 Internal Server Error GET http://localhost:3000/posts/timeline/(objId) 500 (Internal Server Error) - Uncaught (in promise) AxiosError {'Request failed with status code 500' - GET http://localhost:3000/posts/timeline/(objId) 500 (Internal Server Error) - Uncaught (in promise) AxiosError {'Request failed with status code 500' GET方法返回500内部服务器错误 - GET method returns 500 Internal server error GET URL 500(内部服务器错误)- Heroku - GET URL 500 (Internal Server Error) - Heroku Postman 获取调用 500 内部服务器错误 - Postman Get call 500 internal server error GET 500(内部服务器错误)laravel / js - GET 500 (Internal Server Error) laravel/js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM