我在我的網站上遇到了ajax調用問題。用ajax和jquery獲取數據
我想用jquery和ajax對象從數據庫中動態加載我的頁面。
但他們不返回,我不知道問題來自PHP或我的jQuery腳本。
這裏是我的表:
- ID(INT,PK,AI)
- 標籤(焦炭)
- 標題(焦炭)
- 內容(焦炭)
這裏是我的PHP腳本(get_page.php):
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=wemusic;charset=utf8', 'root', 'root');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$reponse = $bdd->prepare('SELECT title, content FROM page WHERE label = ?');
$reponse->execute(array($_GET['label']));
$data = $reponse->fetch();
echo $data;
?>
我的第一個問題是:echo
是否可以使用數組?或者我應該使用echo json_encode?
而且還有我的jQuery腳本:
$("document").ready(function(){
var bloc = $(".third").hide();
var trigger = $(".presta-link");
trigger.click(function(e){
e.preventDefault();
page = $(this).data('page');
$.ajax({
url: 'get_page.php',
type: 'get',
data: {label: page},
success: function (data) {
bloc.show();
$(".presta-title").empty().html(data.title);
$(".presta-description").empty().html(data.content);
},
error:function(jqXHR)
{
console.log(jqXHR);
}
});
});
});
另外:jQuery是加載,和我的鏈接都有着data-page
值。
但我沒有得到任何迴應。沒有PHP錯誤,沒有jqXHR消息。
要回答有關回聲的問題,它不適用於數組(拋出通知,數組到字符串轉換)。對於主要問題,請參閱@ Barmar的回答 –