简体   繁体   中英

My php callback is printing random html source code

I have a post request which querys a database and is supposed to return an array of objects. When i return this in json format it will not let me JSON.parse because the return content includes the whole html shell for the webpage, i am unsure why this is. is there anything i could be missing. Here is my code:

getLogins.php

    $servername= "localhost";
    $sql_username = "root";
    $sql_password = "";
    $dbname = "website_login";
    $conn = mysqli_connect($servername, $sql_username, $sql_password, $dbname);

    $sql = "select * from login;";
    $result = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_array($result)){
        
      array_push($loginInfo, array("Username"=> $row["Username"],"Password"=> $row["Password"] , "First_name"=> $row["First_name"], "Second_name"=> $row["Second_name"], "Email"=> $row["Email"]));
      echo json_encode($loginInfo);
    }
    
    mysqli_close($conn);

login.php

  $("#loginButton").on("click", login);


  function login() {

    $.post("getLogins.php", function(responseData){
      console.log(responseData);
      let logins = JSON.parse(responseData);
      console.log(logins);
    });
    
  }
  

</script>

Maybe it's not just random HTML but an error instead?

Check the error by adding these lines to the top of your code:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Then, read the response yourself instead of attempting to parse it.

From what I can see, the variable $loginInfo could be the problem as it is not declared and you are attempting to push another array to it

because the return content includes the whole html shell for the webpage

Because that is what getLogins.php generates (even if you haven't shown us all of getLogins.php .

PHP doesn't stop sending all the data outside <?php... ?> just because the request was initiated by JavaScript instead of traditional browser navigation.

If you want to design an API, then design an API. The server-side code should only output the data you want to output.

If you have code that generates HTML inside getLogins.php , remove it. If your wrapper is being added because you are using the front controller pattern then exclude your API endpoints from it.

As others have noted, you have not actually provided the full getLogins.php file, which makes this difficult.

However, if the code you provided is at the very beginning of the actual getLogins.php file, if you were to place a die(); statement at the end (just after your call to mysqli_close($conn); ), that should do the trick.

It looks like there are other errors with your code as well, but hopefully this can get you pointed in the right direction:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM