我正在尝试使用 javascript 从 MySQL 数据库中获取一些元素,fetch如下所示:
function fetchPrices(){
fetch('getPricingPlans.php', {
method:'POST',
body: new URLSearchParams('size=' + size)
})
.then(res => res.json())
.then(res => viewPrices(res))
.catch(e => console.error('Error: ' + e))
}
下面是getPricingPlans.php调用另一个 PHP 文件livesearch,该文件是一个构造函数连接到数据库的类,并且是加载页面时调用的第一件事。
<?php
require_once('livesearch.php');
$size = $_POST['size'];
$con = new livesearch();
$data = $con->getPrices($size);
echo json_encode($data);
?>
接下来getPrices()来自livesearch.php:
public function getPrices($size){
$query = "SELECT price FROM PricePlans WHERE size = size";
$stmt = $this->con->prepare($query);
$stmt->execute(["size" => $size]);
// to put values in an associative array with value mapped to field name
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $data;
}
PricePlans是我在数据库中的表,其中包含一个名为price.
问题是我在fetch执行时收到以下错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
一些发现:
如果我将 更改fetch()为GET,并在getPricingPlans.php(不进入livesearch.php)中回显一个简单的字符串,错误就会消失。
其次,我已经让这个fetch()在同一个 PHP 页面中使用不同的不同的表格使用相同的格式。是否有第二次调用(实例)到 livesearch.php 是问题所在?
偶然的你