ajax XMLHttpRequest.status returns unexpected value
why in this simple code xmlHttp.status
returns 404 as not found?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajax.aspx.cs" Inherits="ajax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function startRequest() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "simpleResponse.xml", true);
xmlHttp.send(null);
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
alert(xmlHttp.status);
if (xmlHttp.status == 200) {
alert("The server replied with: " + xmlHttp.responseText);
}
}
}
</script>
</head>
<body>
<form action="#">
<inp开发者_C百科ut type="button" value="Start Basic Asynchronous Request" onclick="startRequest();" />
</form>
</body>
</html>
404 is File Not Found. So, your requested URL is not available. Check is simpleResponse.xml
accessible? if it is, try to use full URL: http://blablabla.com/simpleResponse.xml
Check The status in the onLoad hook, like so.
fileRequest.onload = function( e ) {
// Check the status to make sure there isnt a 404 etc
switch( this.status ){
case 400, 404:
// do 404 stuff
break;
default:
break;
}
}
};
精彩评论