【问题标题】:Unable to retrieve data using jQuery.post无法使用 jQuery.post 检索数据
【发布时间】:2009-04-16 12:52:08
【问题描述】:

我正在尝试使用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 示例有以下行: $.post("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":"bs.byporten@brandstad.no","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,用于测试)

【问题讨论】:

    标签: php javascript jquery


    【解决方案1】:

    去掉“storeID”周围的引号:

    错误:

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

    对:

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

    【讨论】:

      【解决方案2】:

      bartclaeys 是正确的。就像现在一样,您实际上是在传递字符串“storeID”作为商店 ID。

      但是,还有一些注意事项:

      • 设置storeID: storeID 可能看起来很奇怪——为什么只评估第二个?当我刚开始时,每次我没有发送“1:1”或其他东西时,我都必须三重检查。但是,当您使用这样的对象表示法时,不会评估键,因此只有第二个是实际的变量值。
      • 不,考虑到 JS 文件的位置,将 PHP 文件称为 ../ 是不正确的。您必须根据加载了此 javascript 的任何页面调用它。因此,如果页面实际上与您正在调用的 PHP 文件位于同一目录中,您可能需要修复它以指向正确的位置。
      • 有点与前面的观点有关,您真的想了解Firebug。这将允许您查看 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":"bs.byporten@brandstad.no",
          "opening_hours":"Man-Fre 10-21, L",
          "active":"pending"
        }
      ]
      

      这实际上是一个包含单个对象(花括号)的数组(方括号)。

      所以当你尝试这样做时:

      alert(data.name);
      

      这是不正确的,因为对象作为数组的第一个元素存在。

      alert(data[0].name);
      

      应该如您所愿。

      【讨论】:

      • 我尝试过使用 Firebug。但不明白我如何调试 javascripts :(
      • 嗯..没关系。找到了。帖子:我的 sID 未定义响应:包含 JSON 表示法的商店信息。
      【解决方案3】:

      您的 JSON 以 javascript 数组的形式返回...其中 [] 包含花括号 [{}]

      这样就可以了。

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

      希望对您有所帮助。 D

      【讨论】:

        【解决方案4】:

        好的,多亏了 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;
        ?>
        

        感谢大家的帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多