dongzhan0624 2012-12-07 00:52
浏览 45

为什么这个jquery ajax调用没有从我的PHP脚本返回数据?

I'm new to jQuery, and have not been able to debug this ajax call in Firebug:

This is my ajax call:

var styndx = $('#studylist option:selected').val();
var studyname = $('#edit_field').val();


$.post("saveStudyName.php", {'type': 'update', 'studyname':studyname, 'styndx':styndx},
    function(resultmsg) {
    $('#edit_field').val('');
    $('#savebtn').attr('disabled',true);
    refresh_studynames();
});

And this is the function refresh_studynames:

function refresh_studynames()
{
  $.ajax({                                      
     url: 'getStudyNames.php',                  
     data: "",                                                             
     dataType: 'json',               
          error: function() {
            alert('Refresh of study names failed.');
          },
     success: function(data)
     {
        $data.each(data, function(val, sname) {
        $('#studylist').append( $('<option></option>').val(val).html(sname) )
      });
     } 
  });
}

Finally, this is the php script getStudyNames.php ($dbname,$dbconnect, $hostname are all populated, and $dbconnect works; the backend database is Postgres, and pg_fetch_all is a Postgres function in PHP that returns result as an array):

$dbconnect = pg_pconnect("host=".$hostname." user=".$dbuser." dbname=".$dbname);    
    if (!$dbconnect)    {
        showerror(0,"Failed to connect to database",'saveStudyName',30,"username=".$dbuser.", dbname=".$dbname);
        exit;
    }

    $sql = "SELECT ST.studyindex,ST.studyabrv AS studyname
            FROM ibg_studies ST
            ORDER BY studyname";


    $fetchresult = pg_exec($dbconnect, $sql);
    if ($fetchresult) {
        $array = pg_fetch_all($fetchresult);
        echo json_encode($array);
    } else {
        $msg = "Failure! SQL="+$sql;
        echo $msg;
    }

Any help much appreciated....

  • 写回答

1条回答 默认 最新

  • dongpian4954 2012-12-07 02:09
    关注

    The line

    $('#studylist').append( $('<option></option>').val(val).html(sname) );
    

    looks wrong.

    I'm not too sure but you could try :

    var $studylist = $('#studylist').empty();
    $data.each(data, function(i, record) {
        $studylist.append( $('<option/>').html(record.sname) );
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)