1

I would like to create an example for posting, which would be a self-contained HTML file, that could emulate an AJAX call locally (that is, if the file is on your local PC file system, and it opens in your browser as file:///path/to/file.htm).

I have arrived at the following example, which doesn't work. What I want to do there, is click on the "Get Data!" button, which initiates a request to the same page with a query string parameter shdat; then in JS, if the page detects the query string shdat, it should stop further loading of the page, and replace the current document as a string (the data we're "sending"), so that the "asker" gets this as the response of the AJAX call (and ultimately shows it in div dataholder).

Here is my example:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <style type="text/css">
.my_btn { background-color:yellow; }
.dispnone { display:none }
  </style>
  <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script type="text/javascript">

var thishref = window.location.href.slice(0, window.location.href.indexOf('?')+1);
var qstr = window.location.href.slice(window.location.href.indexOf('?')+1);
console.log("qstr", qstr);
if (qstr == "shdat") {
  // interrupt normal page loading:
  window.stop();
  // replace entire page so far with our "data",
  // which will effectively "send it back" to "asker"?
  // (doesn't work)
  $(document).html("Sending some message");
}

function OnGetdata(inbtn) {
  console.log("OnGetdata");
  // http://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url

  $.ajax(thishref + "?shdat", {
    success: function(data) {
      console.log("data ", data);
    },
    error: function(xhr, ajaxOptions, thrownError) {
      console.log("error " + thishref + " : " + xhr.status + " / " + thrownError);
    }
  });

}

ondocready = function() {
  $("#getdata").click(function(){
    OnGetdata(this);
  });
}
$(document).ready(ondocready);
  </script>
</head>

<body>
  <h1>Hello World!</h1>
  <p>Here is the test:</p>

  <button type="button" id="getdata" class="my_btn">Get Data!</button>
  <div id="dataholder"></div>

</body>
</html>

The thing is, the Ajax call here will complete, but the console.log("data ", data); will show an XMLDocument, which is basically the whole page as it is - instead of the data only.

Is something like this possible to do, and if so, how?

sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • 3
    Anything you wish to do locally via a script is usually barred by security measures on your browser. They simply won't allow local file access(most of the time). if you wish to simulate locally, install a server like apache, it's fast to set up, and then you have a good simulator. If you can't install apache consider coding a native application(#activex #internetexplorer #securitybadidea) – Tschallacka May 26 '16 at 14:23
  • Thanks for that, @MichaelDibbets - I didn't even think of the security considerations, good to keep that in mind! I just thought that in the `file://` protocol, there is nothing there to really do interpreting, so I managed to do what I want in PHP (posted as answer below). – sdbbs May 26 '16 at 14:36

1 Answers1

2

Well, I was thinking a bit about this, and I guess, the problem is when a call is made to file:///, there is simply nothing that can interpret data - it would be like doing cat somefile.txt on Unix; you just get the raw contents, cat in itself cannot do any processing.

So, when the Ajax call runs, it simply reads the whole file as a text string, which is not interpreted by anything (including the browser), and so the JS switch I have there would basically never run.

A quick workaround would be to use PHP; and have the switching part (which reacts on presence of the query string) in PHP (specifically php-cli as it is called on Ubuntu/Debian, which may have a server feature switch -S, but is otherwise not a full PHP install); then, if your file is in ~/Desktop/tmp.php, one can simply run

php -S localhost:8080

... in the same folder (~/Desktop), and then visit in a browser: http://localhost:8080/tmp.txt. This is how the OP file should be changed, so it works as expected under PHP - we can call it tmp.php now:

<?php

if ($_SERVER["QUERY_STRING"] == "shdat") {
  echo "Sending some message";
  exit;
}

?>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <style type="text/css">
.my_btn { background-color:yellow; }
.dispnone { display:none }
  </style>
  <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
  <script type="text/javascript">

var thishref = window.location.href.slice(0, window.location.href.indexOf('?')+1);
var qstr = window.location.href.slice(window.location.href.indexOf('?')+1);
//~ console.log("qstr", qstr);
//~ if (qstr == "shdat") {
  //~ // interrupt normal page loading:
  //~ window.stop();
  //~ // replace entire page so far with our "data",
  //~ // which will effectively "send it back" to "asker"?
  //~ // (doesn't work)
  //~ $(document).html("Sending some message");
//~ }

function OnGetdata(inbtn) {
  console.log("OnGetdata");
  // http://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url

  $.ajax(thishref + "?shdat", {
    success: function(data) {
      console.log("data ", data);
    },
    error: function(xhr, ajaxOptions, thrownError) {
      console.log("error " + thishref + " : " + xhr.status + " / " + thrownError);
    }
  });

}

ondocready = function() {
  $("#getdata").click(function(){
    OnGetdata(this);
  });
}
$(document).ready(ondocready);
  </script>
</head>

<body>
  <h1>Hello World!</h1>
  <p>Here is the test:</p>

  <button type="button" id="getdata" class="my_btn">Get Data!</button>
  <div id="dataholder"></div>

</body>
</html>

Now, when I click the "Get Data!" button, I get the "data Sending some message" in the JavaScript console, which is what I wanted the OP to do...

sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • 1
    If you installed PHP locally, you might as well install xampp and get the entire (L)AMP stack... Then you can do it all - HTML, PHP, and even AJAX :) – cssyphus May 26 '16 at 14:37
  • Thanks @gibberish - on Ubuntu/Debian, there is a standalone package `php-cli` that supports the server feature (after 5.3 or 5.4, cannot remember) but is only intended for a command line, it's not the full PHP - so I'm actually quite happy that I can do this, without having a whole LAMP stack installed `:)` – sdbbs May 26 '16 at 14:39
  • 1
    Sorry, didn't realize you were on Linux... apologies. – cssyphus May 26 '16 at 14:41