檢索數據我試圖使用jQuery.post()函數檢索一些數據。但是 我沒有輸出。無法使用jQuery.post
我有一個顯示錶格的HTML。點擊這個表應該會觸發一個jQuery.post事件。
我的腳本文件看起來是這樣的:
jQuery(document).ready(function() {
jQuery('#storeListTable tr').click(function() {
var storeID = this.cells[0].innerHTML; //This gets me the rowID for the DB call.
jQuery.post("../functions.php", { storeID: "storeID" },
function(data){
alert(data.name); // To test if I get any output
}, "json");
});
});
我的PHP文件看起來像這樣:
<?php
inlcude_once('dal.php');
//Get store data, and ouput it as JSON.
function getStoreInformation($storeID)
{
$storeID = "9";//$_GET["storeID"];
$sl = new storeLocator();
$result = $sl->getStoreData($storeID);
while ($row = mysql_fetch_assoc($result)) {
{
$arr[] = $row;
}
$storeData = json_encode($arr);
echo $storeData; //Output JSON data
}
?>
我已經測試過的PHP文件,它以JSON格式輸出數據。我現在唯一的問題是將這些數據返回給我的JavaScript。
- 由於javascript位於/ js /文件夾中,使用'../'調用php文件是否正確?
- 我不認爲我正確傳遞了storeID參數。什麼是正確的方式?
- 如何調用getStoreInformation($ storeID)函數並傳遞參數?在jQuery.com jQuery的例子有以下行:$。員額( 「test.php的」,{FUNC: 「getNameAndTime」} 是在getNameAndTime在test.php的函數的名稱
? 。
我已經得到了一步 我從()函數內移動的代碼,外部所以現在的PHP代碼在執行文件運行
我的js腳本現在看起來是這樣的。:
jQuery('#storeListTable tr').click(function() {
var storeID = this.cells[0].innerHTML;
jQuery.post("get_storeData.php", { sID: storeID },
function(data){
alert(data);
}, "text");
});
這會產生一個警報窗口,以JSON格式將商店數據作爲字符串輸出。 (因爲我已將「json」更改爲「text」)。
JSON字符串看起來是這樣的:
[{"id":"9","name":"Brandstad Byporten","street1":"Jernbanetorget","street2":null,"zipcode":"0154","city":"Oslo","phone":"23362011","fax":"22178889","www":"http:\/\/www.brandstad.no","email":"[email protected]","opening_hours":"Man-Fre 10-21, L","active":"pending"}]
現在,我真正想要的,是從JSON輸出中的數據。 因此,我會將「text」更改爲「json」和「alert(data)」更改爲「alert(data.name)」。 所以現在我的js腳本看起來就像這樣:
jQuery('#storeListTable tr').click(function() {
var storeID = this.cells[0].innerHTML;
jQuery.post("get_storeData.php", { sID: storeID },
function(data){
alert(data.name);
}, "json");
});
不幸的是,唯一的輸出我得到的,是「不確定」。 如果我改變「alert(data.name);」到「alert(data);」,輸出是「[object Object]」。
那麼如何輸出商店的名稱?
在PHP文件中,我試過設置$ storeID = $ _GET [「sID」];但我不會看重價值。我怎樣才能得到在jQuery.post中作爲參數傳遞的值? (目前我已經硬編碼了STOREID,用於測試)
我試過使用Firebug。但不明白我怎麼能調試javascripts :( – Steven 2009-04-16 14:08:45
嗯..沒關係,發現它。 帖子:我的sID是undefined 響應:包含商店信息以JSON記法 – Steven 2009-04-16 14:10:52