你可以做這樣的事情(在此服務器端使用PHP,但可以是任何東西):
<form onsubmit="javascript:do_submit(); return false;" method="post" >
....
</form>
<script type="text/javascript">
function do_submit()
{
var params = {"param1":$("elem1").val(), "param2":$("elem2").val(), ....};
$.ajax
({
url:"ajax_handler.php",
type:"POST",
data:params,
success: function(retval)
{
var err_code;
if (! $.browser.msie)
{
if ($('error_code:first',retval))
{
err_code = $('error_code:first',retval).text();
//check err_code and show error, or redirect to a different page ,or close window
}
else
{
//bad xml, show error message
}
}
else // we need to load xml differently in case of IE
{
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
try
{
xml.loadXML(retval);
err_code = xml.documentElement.getElementsByTagName('error_code')[0].text;
//check err_code and show error, or redirect to a different page ,or close window
}
catch (error)
{
//bad xml, show error message
}
}
}
});
}
</scirpt>
Php:
<?php
$xml = new DOMDocument("1.0", "UTF-8");
$xml_root = $xml->createElement("response");
$xml->appendChild($xml_root);
$xml_code = $xml->createElement("error_code");
$xml_root->appendChild($xml_code);
// process request
// ....
$xml_val = $xml->createTextNode(utf8_encode(0)); //
$xml_code->appendChild($xml_val);
print($xml->saveXML());
?>
我認爲我們會設置一個標誌在會話和輸出隱藏的表單變量時,一切都已經成功,然後關閉窗口。 – RyanP13 2010-03-09 08:54:31