2009-04-16 52 views
0

檢索數據我試圖使用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。

  1. 由於javascript位於/ js /文件夾中,使用'../'調用php文件是否正確?
  2. 我不認爲我正確傳遞了storeID參數。什麼是正確的方式?
  3. 如何調用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]」。

  1. 那麼如何輸出商店的名稱?

  2. 在PHP文件中,我試過設置$ storeID = $ _GET [「sID」];但我不會看重價值。我怎樣才能得到在jQuery.post中作爲參數傳遞的值? (目前我已經硬編碼了STOREID,用於測試)

回答

0

好的,感謝Darryl,我找到了答案。

因此,這裏是功能代碼的人誰想知道這一點:

JavaScript文件

jQuery(document).ready(function() { 

    jQuery('#storeListTable tr').click(function() { 

    jQuery.post("get_storeData.php", { storeID: this.cells[0].innerHTML },  // this.cells[0].innerHTML is the content ofthe first cell in selected table row 
     function(data){ 
     alert(data[0].name); 
     }, "json"); 
    }); 

}); 

get_storeData.php

<?php 
    include_once('dal.php'); 

    $storeID = $_POST['storeID']; //Get storeID from jQuery.post parameter 

    $sl = new storeLocator(); 
    $result = $sl->getStoreData($storeID); //returns dataset from MySQL (SELECT * from MyTale) 

    while ($row = mysql_fetch_array($result)) 
    { 
    $data[] = array( 
    "id"=>($row['id']) , 
    "name"=>($row['name'])); 
    } 

    $storeData = json_encode($data); 

    echo $storeData; 
?> 

感謝您的幫助夥計們!

6

失去了引號 「STOREID」:

錯誤:

jQuery.post("../functions.php", { storeID: "storeID" }

右:

jQuery.post("../functions.php", { storeID: storeID }

6

bartclaeys是正確的。就像現在一樣,您實際上將字符串「storeID」作爲商店ID傳遞。

然而,一對夫婦更注意事項:

  • 這似乎不可思議,你將設置storeID: storeID - 爲什麼只被評估的第二個?當我第一次開始的時候,每次我不發送「1:1」或什麼的時候,我都必須進行三重檢查。然而,當你使用象這樣的對象表示法時,鍵不會被評估,因此只有第二個鍵是實際的變量值。
  • 不,這是不正確的,你正在調用PHP文件作爲../思考JS文件的位置。你必須調用它關於這個JavaScript加載的任何頁面。因此,如果該頁面與您正在調用的PHP文件實際位於相同的目錄中,則可能需要修復該問題以指向正確的位置。
  • 把以前的幾點綁在一起,你真的想把你的手放在Firebug。這將允許您在發送AJAX請求,成功創建AJAX請求,向其發送什麼數據以及正在發送什麼數據時查看AJAX請求。簡單地說,就是選擇調試你的Javascript/AJAX應用程序的共識工具,你應該擁有它,使用它,並珍惜它,如果你不想浪費另外6天來調試一個愚蠢的錯誤。 :)

編輯至於您的回覆,如果你打破你正在返回什麼:

[ 
    { 
    "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" 
    } 
] 

這實際上是包含單個對象的數組(方括號)(捲曲括號中)。

所以,當你嘗試做:

alert(data.name); 

因爲對象所在的數組的第一個元素這是不正確的。

alert(data[0].name); 

應該按照您的預期工作。

+0

我試過使用Firebug。但不明白我怎麼能調試javascripts :( – Steven 2009-04-16 14:08:45

+0

嗯..沒關係,發現它。 帖子:我的sID是undefined 響應:包含商店信息以JSON記法 – Steven 2009-04-16 14:10:52

1

你的JSON返回爲一個JavaScript數組與... []包裹捲曲位[{}]

所以這是可行的。

wrong: alert(data.name); 
right: alert(data[0].name); 

希望有幫助。 D