Ste*_*ven 0 javascript php jquery
我正在尝试使用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");
});
});
Run Code Online (Sandbox Code Playgroud)
我的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
}
?>
Run Code Online (Sandbox Code Playgroud)
我测试了PHP文件,它以JSON格式输出数据.我现在唯一的问题是将此数据返回到我的javascript.
我更进了一步.我已将代码从函数()内部移到外部.所以现在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");
});
Run Code Online (Sandbox Code Playgroud)
这会生成一个警报窗口,将存储数据输出为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":"bs.byporten@brandstad.no","opening_hours":"Man-Fre 10-21, L","active":"pending"}]
Run Code Online (Sandbox Code Playgroud)
现在,我真正想要的是从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");
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,我得到的唯一输出是"Undefined".如果我改变"alert(data.name);" "alert(data);",输出为"[object Object]".
那么如何输出商店的名称?
在PHP文件中,我尝试设置$ storeID = $ _GET ["sID"]; 但我不认为这个价值.如何在jQuery.post中获取作为参数传递的值?(目前我已经硬编码了storeID,进行测试)
丢失"storeID"周围的引号:
错误:
jQuery.post("../functions.php", { storeID: "storeID" }
对:
jQuery.post("../functions.php", { storeID: storeID }
bartclaeys是正确的.就像现在一样,您实际上是将字符串"storeID"作为商店ID传递.
但是,还有几个笔记:
storeID: storeID- 为什么只有第二个被评估?当我第一次开始时,每次我不发送"1:1"或其他东西时我都要三重检查.但是,当您使用这样的对象表示法时,不会计算键,因此只有第二个键将是实际的变量值.../考虑JS文件的位置而调用PHP文件是不正确的.您必须在加载此javascript的任何页面上调用它.因此,如果页面实际上与您调用的PHP文件位于同一目录中,则可能需要将其修复为指向正确的位置.编辑就你的回复而言,如果你打破了你的回归:
[
{
"id":"9",
"name":"Brandstad Byporten",
"street1":"Jernbanetorget",
"street2":null,
"zipcode":"0154",
"city":"Oslo",
"phone":"23362011",
"fax":"22178889",
"www":"http:\\/www.brandstad.no",
"email":"bs.byporten@brandstad.no",
"opening_hours":"Man-Fre 10-21, L",
"active":"pending"
}
]
Run Code Online (Sandbox Code Playgroud)
这实际上是一个包含单个对象(花括号)的数组(方括号).
所以,当你尝试做:
alert(data.name);
Run Code Online (Sandbox Code Playgroud)
这是不正确的,因为该对象作为数组的第一个元素.
alert(data[0].name);
Run Code Online (Sandbox Code Playgroud)
应该像你期望的那样工作.