为什么这个jquery ajax调用不从我的PHP脚本返回数据? [英] Why is this jquery ajax call not returning data from my php script?

查看:100
本文介绍了为什么这个jquery ajax调用不从我的PHP脚本返回数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,并且无法在Firebug中调试此ajax调用:

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

这是我的ajax调用:

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();
});

这是函数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) )
      });
     } 
  });
}

最后,这是php脚本getStudyNames.php($ dbname,$ dbconnect,$ hostname都已填充,并且$ dbconnect起作用;后端数据库是Postgres,而

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....

推荐答案

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

看起来不对.

我不太确定,但是您可以尝试:

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) );
});

这篇关于为什么这个jquery ajax调用不从我的PHP脚本返回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆