jquery ajax调用从MySql表中设置选择选项

我有一个DDL(#engine)需要根据所选的#make ddl通过MySql表设置其选项值。

我有下面的脚本传递给php文件 – 我知道php文件很好,因为我可以独立输出查询的结果。

但是,我无法根据返回的数组获取更新选项值…可能是使用GET的结果? 这是我第一次尝试通过AJAX返回(通常只是用它来更新数据表等)

我的剧本中有什么不对劲吗?

$(document).ready(function() { $("select#make").change(function(){ $('#engine').find('option').remove().end(); //clear the engine ddl var make = $(this).find("option:selected").val(); //Need the value not the text $.ajax({ url:'get-engine.php', type:'GET', data:{engine:make}, dataType:'json', cache:false, success:function(data){ var ddl = document.getElementById('engine'); for(var c=0;c<obj.length;c++) { var option = document.createElement('option'); option.value = obj[c]; option.text = obj[c]; ddl.appendChild(option); } }, error:function(jxhr){ alert(jxhr.responseText); } }); }); }); 

和get-engine.php ..

 $make = $_GET['engine']; $query= ("SELECT * FROM tb_engine where make_id='$make'"); $result = mysql_query($query); $temp = array(); while ($row = mysql_fetch_assoc($result)) { if(empty($temp)) { $temp=array($row['engine_type']); } else { array_push($temp,$row['engine_type']); } } echo (json_encode($temp)); ?> 

它到目前为止拒绝更新,不能完全找到原因?

提前感谢所有建议

你可以试试

 //json is the json you have recieved in the success handler $("#engine").empty(); $.each( json, function( index, item ) { $("",{value:item,text:item}).appendTo("#engine"); }); 

DEMO