0

这个脚本过去可以正常工作,所以我很困惑为什么它现在不能正常工作;但它在一个简单的 php 调用中使用 .post 来检索给定两个键 mndx 和 sndx 的数据记录,这两个键都包含有效数据。

 function fetchgenotype() {
    // here's where we use an Ajax function to fetch the allele values without reloading the page.
    // Get the index number of the genotype record to retrieve; retrieve it; populate the alleles.

    var mndx, sndx = 0;

    mndx = $('#marker').val();

    if (Study_selected) {
      sndx = $('#stylabid').val();
    } else { 
      sndx = $('#pjtlabid').val();
    }    

    // for debugging...    
    //alert('sndx='+sndx+', mndx='+mndx);

    // This is the cryptic jQuery 'magic' that fetches the genotype values when the
    // Fetch button is clicked, and displays the values fetched.

    $.post("fetchAlleles.php", {mndx: mndx, sndx: sndx},

      function(result) {

    // The .each method used on the return object is the key to displaying multiple
    // run dates, and genotype values, associated with a sample. 

    i=0;
    $.post("fetchAlleles.php", {'mndx': mndx, 'sndx': sndx},



function(result) {
    // this works for a single return value
    //$('#allele_1_1').get(0).value = result[0].allele1;
    //$('#allele_1_2').get(0).value = result[0].allele2;
    //$('#run_date1').get(0).value = result[0].run_date;

    // The .each method used on the return object is the key to displaying multiple
    // run dates, and genotype values, associated with a sample. 

    i=0;
$.each(result,function() {
  if (i==0) {
    $('#allele_1_1').val(this.allele1);
    $('#allele_1_2').val(this.allele2);
    $('#run_date1').val(this.run_date);
  } else if (i==1) {
    $('#allele_2_1').val(this.allele1);
    $('#allele_2_2').val(this.allele2);
    $('#run_date2').val(this.run_date);
  } else if (i==2) {
    $('#allele_3_1').val(this.allele1);
    $('#allele_3_2').val(this.allele2);
    $('#run_date3').val(this.run_date);
  }
  i=i+1;
    })
  }
);
      }

我曾尝试使用 Firebug 进入代码,但在上面的代码中,只要它越过 .post 行,它就会说结果未定义。

php脚本的相关部分是:

 $sql = "SELECT allele1, allele2, run_date FROM geno.genotypes WHERE markers_id=".$mndx." AND gsamples_id=".$sndx;

    // create array to hold results
    $obj_arr = array();
    $fetchresult = pg_exec($dbconnect, $sql);
    if ($fetchresult) {
        while ($obj = pg_fetch_object($fetchresult))
        // Add this object to our array of objects to return
        if (!array_push($obj_arr,$obj)) {
            echo("Error pushing results onto obj_arr");
            showerror(0,"Failed to connect to database",'fetchAlleles',38,"Failed in line 38");
            exit;
        }        
        // for debugging
        //print_r($obj_arr);
    } else {
        echo "(25) Failed to retrieve results.<BR>SQL: ".$sql."</br>";
    }

    // It appears necessary to return the array as json encapsulated.
    echo json_encode($obj_arr);

非常感谢任何帮助!--rixter

4

2 回答 2

1

如果将 post 请求的类型定义为 json,即使出现错误也需要返回 json

您可以在结果函数中使用

console.log(result);
于 2012-09-26T22:25:13.363 回答
1

尝试这个

$.post("fetchAlleles.php", {'mndx': mndx, 'sndx': sndx},

将您的钥匙装在刺中并检查这是否有效..

还要检查浏览器控制台部分中的任何错误。检查请求部分中的参数是什么,如果成功,请检查响应

于 2012-09-26T22:04:28.717 回答