javascript AJAX 不适用于本地主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10634185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX not working on localhost
提问by Colin747
I'm trying to get an AJAX example working but i'm unable to get it working. Are you able to run it on XAMPP fine?
我正在尝试让 AJAX 示例工作,但我无法让它工作。你能在 XAMPP 上运行它吗?
I've three files, message.txt, index.html, ajaxtest.js. When you click on the hyperlink it should bring up a pop up with the contents of message.txt. (all files are in the same directory)
我有三个文件,message.txt、index.html、ajaxtest.js。当您单击超链接时,它会弹出一个包含 message.txt 内容的弹出窗口。(所有文件都在同一个目录下)
index.html
索引.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title> Using XMLHttpRequest</title>
<script type="text/javascript" src="ajaxtest.js"></script>
</head>
<body>
<p>
<a href="message.txt" onclick="grabFile(this.href); return false;">
Click here
</a>
</p>
</body>
</html>
ajaxtest.js
ajaxtest.js
function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) { //see if a XMLHttpRequest exists
xhr = new XMLHttpRequest(); //if it exists change "xhr" to a new instance of the object
}
else if (window.ActiveXObject) { //see if ActiveX exists (for IE)
try { //Allows newer versions of IE to use the newer ActiveX object
xhr = new ActiveXObject("Msxml2.AMLHTTP"); //if it exists change "xhr" to a new instance of the object
}
catch(e) { //catches an error and resets "xhr" to false
try { //Allows older versions of IE to fall back onto the older version using "try...catch"
xhr = new ActiveXObject("Microsoft.XMLHTTP"); //if it exists change "xhr" to a new instance of the object
}
catch(e) {
xhr = false;
}
}
}
return xhr;
}
function grabFile(file) {
var request = getHTTPObject();
if (request) {
request.onreadystatechange = function() {
displayResponse(request);
};
request.open("GET", file, true);
request.send(null);
}
}
function displayResponse(request) {
if (request.readyState == 4) {
if (request.status == 200 || request.status == 304) {
alert(request.responseText);
}
}
}
采纳答案by Vik
As I have tried with your example , your request object is not hitting state 4 and using again . check request.status before the below code, its getting 0 value always . And that's why you script is not alerting response .
正如我在您的示例中尝试过的,您的请求对象未达到状态 4 并再次使用。在下面的代码之前检查 request.status ,它总是得到 0 值。这就是为什么你的脚本没有提醒 response 。
if (request.status == 200 || request.status == 304) {
alert(request.responseText);
}
Check this for your solutions : http://www.ibm.com/developerworks/web/library/wa-ajaxintro3/
检查您的解决方案:http: //www.ibm.com/developerworks/web/library/wa-ajaxintro3/
Hope that help .
希望有所帮助。
回答by Kamyar Nazeri
For security reasons JavaScript's access to the file system on the client is restricted! you can read more here
出于安全原因,JavaScript 对客户端文件系统的访问受到限制!你可以在这里阅读更多
If you are using google chrome you have to startup the executable with -allow-file-access-from-files
command:
如果您使用的是谷歌浏览器,则必须使用以下-allow-file-access-from-files
命令启动可执行文件:
chrome.exe -allow-file-access-from-files
There might be a similar configuration for FireFox
FireFox 可能有类似的配置
Update:
reviewing your code, i figures you are comparing request.status
against OK
(200), and Not Modified
(304) headers! These are HTTP response headers returned from a web server, so when you run the code on a web server you mustcheck response headers, otherwise, when running locally using file:///
protocol you always get 0
as for the response header
更新:查看您的代码,我认为您正在request.status
与OK
(200) 和Not Modified
(304) 标头进行比较!这些是从 Web 服务器返回的 HTTP 响应标头,因此当您在 Web 服务器上运行代码时,您必须检查响应标头,否则,当使用file:///
协议在本地运行时,您总是会得到0
响应标头
Here's a workaround, you can check the domain, if you are running locally then no need to check for response status:
这是一种解决方法,您可以检查域,如果您在本地运行,则无需检查响应状态:
function displayResponse(request) {
if (request.readyState == 4) {
if (!document.domain || request.status == 200 || request.status == 304) {
alert(request.responseText);
}
}
}