我试图从ajax调用加载下拉列表。这是我的代码:
<select name="customer" onChange="testAJAX(this.value)">
<option value="0" selected >Select Customer</option>
<?php
$services = db_query('SELECT cust_id,cust_name FROM '.CUST_TABLE.' ORDER BY cust_name');
while (list($cust_id,$name) = db_fetch_row($services)){
$selected =($info['customer']==$cust_id)?'SELECTED':'';?>
<option value="<?=$cust_id?>"<?=$selected?>><?=$name?></option>
<?php
}?>
</select>
Javascript功能
function testAJAX(custID) {
$('#sLocation').empty();
var myURL = "getLocations.php?custID=" + custID;
$.ajax({
type:"POST",
url: myURL,
contentType:"application/json; charset=utf-8",
data: { custID : custID },
dataType:"json",
success: function(data){
document.getElementById("sLocation").innerHTML = data;
},
error: function (xhr, textStatus, errorThrown) {
document.getElementById("sLocation").innerHTML = xhr.responseText;
},
complete: function(){
}
});
}
和它调用的页面
<?php
$custID = 0;
$mysqli = new mysqli("localhost", "James", "mypassword", "OSTickets");
if (isset($_REQUEST['custID'])) {
$custID = $_REQUEST['custID'];
}
$sql = 'SELECT l.location_id, l.site_name FROM ost_customer_location'
. ' AS l JOIN ost_customer'
. ' AS c ON c.cust_id=l.cust_id'
. ' WHERE c.cust_id='.$custID;
$locations = array();
$str = "";
$str .= "<option value='0'>Select Location</option>";
if ($result = $mysqli->query($sql)) {
while ($row = mysqli_fetch_array($result)){
$str .= "<option value=".$row["0"].">".$row["1"]."</option>";
}
}
echo $str;
?>
我可以让它工作,但只能在ajax的错误功能而不是成功但是当我提醒xhr.responseText时,它没有显示任何错误,只是选择的html代码是:
<option value='0'>Select Location</option>
<option value='252'>Location 1</option>
<option value='345'>Location 2</option>
我如何弄清楚它投掷的错误?
将错误功能更改为
error: function(jqXHR, textStatus, errorThrown){ console.log(jqXHR); }
这是来自控制台的响应。
对象{readyState:4,responseText:&#34; Duke Rd - Primary&#34;,状态:200,statusText:&#34; OK&#34;}