#ajax #xmlhttprequest
#ajax #xmlhttprequest
Вопрос:
почему в этом простом коде xmlHttp.status
возвращает 404 как не найденный?
<%@ 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="#">
<input type="button" value="Start Basic Asynchronous Request" onclick="startRequest();" />
</form>
</body>
</html>
Ответ №1:
404 — файл не найден. Итак, запрошенный вами URL недоступен. simpleResponse.xml
Доступна проверка? если это так, попробуйте использовать полный URL: http://blablabla.com/simpleResponse.xml
Ответ №2:
Проверьте статус в перехватчике загрузки, вот так.
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;
}
}
};