Ajax code without a web server

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

javascript function:

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 content is HTML content:

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

      

I can get this message in the alert box * inside this do_the_click * method, but I couldn't get the data in the alert box, and so I couldn't get the Java Collections.html page in index.html.

I have searched a lot on google that how to load data locally without using a server in jquery.ajax but doesn't get any good solution, so I think it's okay for others if it's on stackoverflow.

+1


source to share


7 replies


I can run ajax without a webserver, the problem was my code below which I was using on my filesystem and you can use that as well.

      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

+1


source


You cannot do that. if you don't have a server you cannot send ajax, it will be a cross-browser issue. since others say ajax won't work with the protocol file://

, you need a server to call http://

that supports ajax.



+2


source


When you make AJAX requests using standalone html files, the HTTP request to the url is generated by the browser. All you need to do is make sure you have JQuery enabled. Here's what you need to do.

Include JQuery:

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

      

JQuery is located here in js folder. This is included in the standalone html file. Now, to make an AJAX request, use the following code.

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

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

      

+2


source


AJAX creates an HTTP request that the server should respond to. If you don't have a server, you cannot use an AJAX request. You can run the server on your computer, but this is not the same as getting a file from the local file system.

+1


source


You cannot do this. If you don't have a server, you are violating the browser's cross-domain policy as you won't have a domain!

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

+1


source


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

0


source


Now, in the current scenario, it depends on where your server is located. If the server is somewhere else, it will require a CORS request (cross lookup request). So using file: // works fine with CORS.

I came to this question because I was looking for a reason why I can make an ajax request without using a web server. Got an answer Thank you!

0


source







All Articles