Javascript Jquery:在没有网络服务器的情况下在本地运行 AJAX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3397644/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:29:01  来源:igfitidea点击:

Jquery: Running AJAX locally without a webserver

phpjavascriptjqueryajax

提问by Steven

I have the following function in a .js file in index.html

我在 index.html 的 .js 文件中有以下功能

function getValues(){

 $.ajax({
   type: 'POST',
   url: "http://localhost/getData/getdata.php",
   success: function(data){
     var dataValues;
     var apnd;

     dataValues = String(data.NSE);
     apnd = "a";
     updateValues(dataValues, apnd);

     dataValues = String(data.BSE);
     apnd = "b";
     updateValues(dataValues, apnd);
    },
   dataType: "json"
 });

}

this works fine when I run it in a webserver like wamp. But I want to run index.html locally i.e without a webserver, The user just double clicks index.html and it should run but it doesn't. data is always null. What could be the problem? Sorry I am a super JQuery Noob.

当我在像 wamp 这样的网络服务器中运行它时,这很好用。但我想在本地运行 index.html,即没有网络服务器,用户只需双击 index.html 并且它应该运行但它没有。数据始终为空。可能是什么问题呢?对不起,我是一个超级 JQuery 菜鸟。

the code in getdata.php file is

getdata.php 文件中的代码是

<?

echo json_encode(array("NSE"=>rand(5000, 20000),"BSE"=>rand(5000, 20000))); 

?>

回答by Bob Fincheimer

When you run your index.html from a file the AJAX works. But the problem occurs because you are viewing the file at address "file://....../index.html" and you are making a AJAX request to "http://localhost/..../something.php" which IS NOT ALLOWED because of cross site scripting. All AJAX requests must go to the same domain/server.

当您从文件运行 index.html 时,AJAX 可以工作。但是出现问题是因为您正在查看地址为“file://....../index.html”的文件,并且您正在向“ http://localhost/..../something”发出 AJAX 请求。 php",由于跨站点脚本,这是不允许的。所有 AJAX 请求都必须发送到同一个域/服务器。

This is a assuming that you are viewing the file by double clicking it and still making the AJAX request to the web server.

这是假设您正在通过双击文件来查看文件,并且仍然向 Web 服务器发出 AJAX 请求。

回答by Gordon Gustafson

AJAX needs a webserver to communicate with for it to be able to retrieve any data; otherwise its just talking to a wall. Running the script without a webserver is like trying to make a call with no cell-service. :D

AJAX 需要一个网络服务器与之通信才能检索任何数据;否则它只是对着墙说话。在没有网络服务器的情况下运行脚本就像尝试在没有单元服务的情况下进行呼叫。:D

回答by Fosco

The web server is exactly what is handling all of the details for you.

Web 服务器正是为您处理所有细节的地方。

You cannot POST without a web server to post to. HTTP = web protocol, so you cannot have a HTTP URL without a web server to target.

如果没有要发布到的 Web 服务器,则无法发布。HTTP = Web 协议,因此如果没有要定位的 Web 服务器,您将无法获得 HTTP URL。

The web server is also the process that takes your request for a PHP page and runs the PHP interpreter, managing the inputs and outputs.

Web 服务器也是接收您对 PHP 页面的请求并运行 PHP 解释器、管理输入和输出的过程。

Why do you want to run it locally?

为什么要在本地运行?

回答by BGerrissen

Ajax does not work over the file:// protocol as mentioned by others. Perhaps you want something like http://www.appcelerator.com/to create desktop apps with html/js/css

Ajax 不适用于其他人提到的 file:// 协议。也许你想要像http://www.appcelerator.com/这样的东西来使用 html/js/css 创建桌面应用程序

回答by Sarfraz

You can't do that, you should open your html file also from web server address eg http://localhost/yoursite/file.htmlor even remote server url. You need to go through the server/server url.

你不能那样做,你也应该从 web 服务器地址打开你的 html 文件,例如http://localhost/yoursite/file.html甚至远程服务器 url。您需要通过服务器/服务器 url。

回答by tcooc

Read the SOP. Accessing data from a domain other than the current one is blocked for security reasons.

阅读标准操作程序。出于安全原因,阻止从当前域以外的域访问数据。

回答by Andrew Koper

I'm tickled pink with myself because reading the answers people posted about how you can't do AJAX "locally" without a Web server led me to the solution as to how you can do it. With JavaScript, the XMLHttpRequest() object's methods are mostly produced by the browser and you need to leave out the one produced by the Web server (xmlhttp.status == 200). The following works:

我对自己很满意,因为阅读了人们发布的关于如何在没有 Web 服务器的情况下“本地”执行 AJAX 的答案,让我找到了如何做到这一点的解决方案。对于 JavaScript,XMLHttpRequest() 对象的方法主要由浏览器生成,您需要忽略 Web 服务器生成的方法 (xmlhttp.status == 200)。以下工作:

<script>
window.onload = function() {

    var input = document.getElementById("input");

    input.onclick = function() {
        var xmlhttp;
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                document.getElementById("response").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "response.html", true);
        xmlhttp.send();
    }
}
</script>
</head>

<body>
<h3>AJAX Request/Response</h3>
<p></p>
<input id="input" type="button" value="Call AJAX" />
<p></p>
<div id="response"></div>