【发布时间】:2014-10-02 20:46:12
【问题描述】:
我正在尝试使用ajax帖子,以返回一个国家/地区的XML省份列表。
这是我收到的错误消息:
xml 似乎没问题 - 选择一个国家/地区时,错误消息显示正确的州/省,但由于某种原因它出错,我无法使用成功回调函数。 我相信我应该在我的 php 中回显 xml 响应,并且 $(document).ready 函数应该包含 ajaxError 处理函数,但我无法在成功回调中提醒我的省/州列表。有人知道我在这里缺少什么吗?
HTML
<select name="aff_country" onChange="loadStates(this.value);">
<option value="Select Me">Select Me</option>
</select>
Javascript
$(document).ready(function() {
$(document).ajaxError(function(e,xhr,settings,exception) {
alert('Error Page: ' + settings.url + '\n\n'+'Error Message:\n' + xhr.responseText );
});
});
function loadStates(country_value) {
$.post("/states.php", {con_name: country_value}, function(data) {
console.log(data);
},"xml");
}
XML (PHP)
<?
$request_obj = new Request();
$request_obj->flds = $_REQUEST;
$states = array();
$states = $country_bus_obj->states($request_obj,$_CONFIG);
$xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xml .= "<response>";
$xml .= "<status>" . $status . "</status>";
$xml .= "<message>" . $trans->msg . "</message>";
$xml .= "<states>";
foreach ($states as $item) {
$xml .= "<state>";
$xml .= "<name>" . $item['sta_name'] . "</name>";
$xml .= "</state>";
}
$xml .= "</states>";
$xml .= "</response>";
header('Content-type: text/xml');
header('Cache-Control: no-cache');
echo $xml;
?>
【问题讨论】: