简体   繁体   English

将数据从 mysql 传递到 javascript

[英]pass data from mysql to javascript

Is this the right way to retrieve data from mysql using jquery?这是使用 jquery 从 mysql 检索数据的正确方法吗? The php side is working fine ($data gets printed onto the page) but jquery doesn't seem to be receiving the variable at all. php 端工作正常($data 打印到页面上)但 jquery 似乎根本没有接收到变量。

Besides that, is there a way to get the jquery function to run after the page AND after the google maps initMap() function has finished loading?除此之外,有没有办法让 jquery function 在页面之后和谷歌地图 initMap() function 完成加载后运行? Is it possible to include jquery code inside a standard javascript function?是否可以在标准 javascript function 中包含 jquery 代码?

admin.php admin.php

<?php
require 'private/database.php';

$sql = "SELECT * FROM latlng";
$result = mysqli_query($conn, $sql);

$data = array();
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
}
echo json_encode($data);

?><!DOCTYPE html>
<html>
  <head>
    <link type="text/css" rel="stylesheet" href="css/admin.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/admin.js"></script>
    <script type="text/javascript" src="js/maps.js"></script>
    <script defer 
      src="https://maps.googleapis.com/maps/api/js?key=(mykey)&callback=initMap&libraries=places&v=weekly"
    ></script>
  </head>
  <body>
    <div id="map"></div><br>
  </body>
</html>

What I've tried js/admin.js我尝试过的 js/admin.js

$(document).ready(function() {
    $.ajax({
        url: '../admin.php',
        method: 'post',
        dataType: 'json',
        success: function(data) {
            console.log(data);
        }
    })
});

I received a "404 Not found" error in the console我在控制台中收到“404 Not found”错误

The 404-Error indicates that you are using a wrong URL in your jQuery code to get the data. 404 错误表明您在 jQuery 代码中使用了错误的 URL 来获取数据。 Try to enter not just the filename but the whole URL like https://example.com/admin.php for the url parameter. Try to enter not just the filename but the whole URL like https://example.com/admin.php for the url parameter.

Besides your problem getting the data via jQuery, what happens when you open admin.php directly in your browser?除了通过 jQuery 获取数据的问题之外,当您直接在浏览器中打开 admin.php 时会发生什么? Are you getting the $data AND your HTML Code?你得到 $data 和你的 HTML 代码了吗? If thats the case I would recommend you to wrap the whole PHP-Code inside an if-statement:如果是这种情况,我建议您将整个 PHP 代码包装在 if 语句中:

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    header('Content-Type: application/json');
    require 'private/database.php';

    $sql = "SELECT * FROM latlng";
    $result = mysqli_query($conn, $sql);

    $data = array();
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $data[] = $row;
        }
    }
    die(json_encode($data));
}
else{ ?>
<!DOCTYPE html>
<html>
  <head>
    <link type="text/css" rel="stylesheet" href="css/admin.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/admin.js"></script>
    <script type="text/javascript" src="js/maps.js"></script>
    <script defer 
      src="https://maps.googleapis.com/maps/api/js?key=(mykey)&callback=initMap&libraries=places&v=weekly"
    ></script>
  </head>
  <body>
    <div id="map"></div><br>
  </body>
</html>
<? } ?>

Now, if its a POST-Request like from your js, the PHP will return the data as JSON.现在,如果它是来自您的 js 的 POST 请求,PHP 将返回数据为 JSON。 Also the right header will be set.还将设置正确的 header。 If its not a POST-Request the PHP will return your HTML.如果它不是 POST 请求,则 PHP 将返回您的 HTML。

To your other question: Yes, it is possible to use jQuery in a normal JavaScript function.对于您的其他问题:是的,可以在普通 JavaScript function 中使用 jQuery。

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

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