无法使用jQuery.post

问题描述:

检索数据我试图使用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,用于测试)

好的,感谢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; 
?> 

感谢您的帮助伙计们!

失去了引号 “STOREID”:

错误:

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

右:

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

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

你的JSON返回为一个JavaScript数组与... []包裹卷曲位[{}]

所以这是可行的。

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

希望有帮助。 D