PHP使echo json_encode()不在页面上显示信息

时间:2017-03-27 02:48:08

标签: php jquery html sql ajax

我遇到了HTML prize hunt game我想要制作的问题。我遇到的问题是,当我想从我的数据库中获取值时,我必须使用AJAX call on a html page。但为了实现这一目标,我需要回复json_encode(),然后导致某人能够转到该页面(www.website.com/jsonencode.php)并找到奖品的location。 任何帮助表示赞赏:)

PHP代码:

<?php

ini_set('display_errors', 1); 
error_reporting(E_ALL);
$servername = "localhost";
$username = "fkwills";
$password = "password";
$dbname = "FKDatabase";

$conn = mysqli_connect($servername, $username, $password);
mysqli_select_db($conn, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


$query = ("SELECT `markerLocation`, `markerCode`, `used`, `active` FROM `markerInfo`");
$result = mysqli_query($conn, $query);
$result1 = mysqli_fetch_row($result); 

echo json_encode(array("markerLocation"=>$result1[0],"markerCode"=>$result1[1],"used"=>$result1[2], "active"=>$result1[3]));

?>

HTML代码:

var markerCode;
var used;
var active;
var markerLocation;


 function downloadCode() {
        $.ajax({
        type: 'GET',
        url: 'getMarkerPage.php',
        dataType: "json",
        success: function(data) {
            markerCode = data.markerCode;
            used = data.used;
            active = data.active;
            markerLocation = JSON.parse(data.markerLocation);
        },
        error: function(data) {
        console.log(data);
        }
    }); 
}

是的,我调用函数downloadCode();

1 个答案:

答案 0 :(得分:-1)

您可以使用

    $.ajax({
    type: 'POST',
    ...

然后仅当方法是POST

时才回显@ PHP
if ($_SERVER['REQUEST_METHOD'] === 'POST')
    echo json_encode(//............