javascript 没有 Web 服务器的 Ajax 代码

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

Ajax Code without web server

javascriptjqueryhtmlajax

提问by Girish

I am running ajax without a server on my system, I have created one index.html with this

我在我的系统上没有服务器的情况下运行 ajax,我用这个创建了一个 index.html

javascript function:

javascript函数:

function do_the_click(url)
{
alert('inside this method do_the_click');
    $.ajax({
                         async: false,
                         url : url, 
                         contentType: "text/html",
                         type : "GET",
                         dataType : "text/html",                
                         success: function(data){
                         alert('data');
           }});
} 

and my HTML body content is :

我的 HTML 正文内容是:

<a href="Java Collections.html" class="button" id="javacollections" onclick="do_the_click('this.href');"/>Java Collections</a>

I m able to get the this message in alert window *inside this method do_the_click* but not able to get the data in alert window, and so not able to get the page Java Collections.html in the index.html,

我可以在警报窗口 * 在此方法 do_the_click* 中获取此消息,但无法在警报窗口中获取数据,因此无法在 index.html 中获取页面 Java Collections.html,

I have searched very much on google that how to load the data locally without using server in jquery.ajax but doesn't get any good solution, so I think that its good for others as well if it reside on stackoverflow.

我在 google 上搜索了很多关于如何在不使用 jquery.ajax 中的服务器的情况下在本地加载数据但没有得到任何好的解决方案,所以我认为如果它驻留在 stackoverflow 上对其他人也有好处。

采纳答案by Girish

I m able to run the ajax without webserver the problem was with my code the code written below that i had run on my filesystem u can use that as well.

我可以在没有网络服务器的情况下运行 ajax 问题出在我的代码下面,我在我的文件系统上运行的代码你也可以使用它。

      function do_the_click(brl)
      {
          alert('inside this method do_the_click');
          alert(brl);
          var request  =     $.ajax({
                    async: false,
                    url: brl, 
                    contentType: "text/html",
                    type : "GET",
                    dataType : "html",                
                });

                request.done(function( msg ) {
                          alert(msg);
                          $( "#contentarea" ).load( msg, function() {
                                    alert( "Load was performed." );
                          });
                });

                request.fail(function( jqXHR, textStatus ) {
                          alert( "Request failed: " + textStatus );
                });

      }

Java Collections

Java 集合

回答by

You cannot do such thing. if you dont have a server you cannot send ajax it will be a cross browser issue. as the others says ajax will not work with file://protocol you need a server to make an http://call that ajax supports.

你不能做这样的事情。如果您没有服务器,则无法发送 ajax,这将是一个跨浏览器问题。正如其他人所说,ajax 不能使用file://协议,您需要一个服务器来进行http://ajax 支持的调用。

回答by Talha Masood

When you are making AJAX requests using stand alone html files, the HTTP request to the url is made by the browser. All you need to make sure is that you have included JQuery. Here is what you have to do.

当您使用独立的 html 文件发出 AJAX 请求时,对 url 的 HTTP 请求是由浏览器发出的。您只需要确保已包含 JQuery。这是你必须做的。

Include Jquery:

包括jQuery:

<script src="js/jquery-1.10.2.min.js"></script>

The JQuery here resides in the js folder. This is included in the stand alone html file. Now to make the AJAX request use the following code.

这里的 JQuery 位于 js 文件夹中。这包含在独立的 html 文件中。现在使用以下代码发出 AJAX 请求。

$.ajax({
          type: 'GET',
          url:  url, 

          success: function(data){
              console.log(data);
          },
          async:false
      });

回答by Girish

AJAX creates an HTTP request that must be responded to by a server. If you don't have a server you can't use an AJAX request. You can run a server on your computer, but this is not the same as getting a file from the local filesystem.

AJAX 创建一个必须由服务器响应的 HTTP 请求。如果您没有服务器,则无法使用 AJAX 请求。您可以在计算机上运行服务器,但这与从本地文件系统获取文件不同。

回答by Mister Epic

You won't be able to do this. If you don't have a server, you will violate the browser's cross-domain policy, as you won't have a domain!

你将无法做到这一点。如果您没有服务器,您将违反浏览器的跨域策略,因为您将没有域!

AJAX won't work via the filesystem, you need a server.

AJAX 不能通过文件系统工作,你需要一个服务器。

回答by flyandi

This is an older thread but a browser doesn't need to run on a common OS like OSX/Windows/etc .. many people today are making their own browsers from WPE/Wayland or QT5Webkit where same origin policies don't apply.

这是一个较旧的线程,但浏览器不需要在 OSX/Windows/等通用操作系统上运行..今天许多人正在从 WPE/Wayland 或 QT5Webkit 制作自己的浏览器,其中不适用同源策略。

回答by Saurabh Gupta

Now in current scenario it depends upon where your server at. If the server is some place else, it will require CORS (cross origin request). So using file:// with CORS works perfectly.

现在在当前情况下,这取决于您的服务器在哪里。如果服务器在其他地方,它将需要 CORS(跨源请求)。因此,将 file:// 与 CORS 一起使用效果很好。

I came to this question because I was searching for the reason, why I am able to make ajax request without using a web server. Got the answer Thanks!

我来到这个问题是因为我正在寻找原因,为什么我能够在不使用 Web 服务器的情况下发出 ajax 请求。得到答案谢谢!