0

i want to read this txt-file using datatables.net https://www.rapidtech1898.com/aaadownload/arrays.txt

But when i want to use the file with the following files the output is not working i have this error in the chrome inspector: (datatables is not reading as it sould and the inspector shows me that) enter image description here

(at first i had the txt-file locally and read that there are some problems with that using chrome with local file - but this is a "normal" http-link isn´t it? - why is this stil not working as expected?

I also tried to do the same thing locally before - but i get the same error: enter image description here

I have an index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">     
    </head>
    <body>
        <div class="container py-5">
            <header class="text-center text-white">
                <h1 class="display-4">Levermann Scores</h1>
            </header>
            <div class="row py-5">
            <div class="col-lg-10 mx-auto">
                <div class="card rounded shadow border-0">
                <div class="card-body p-5 bg-white rounded">
                    <div class="table-responsive">

                      <table id="example" class="display" style="width:100%">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Position</th>
                                <th>Office</th>
                                <th>Extn.</th>
                                <th>Start date</th>
                                <th>Salary</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th>Name</th>
                                <th>Position</th>
                                <th>Office</th>
                                <th>Extn.</th>
                                <th>Start date</th>
                                <th>Salary</th>
                            </tr>
                        </tfoot>
                      </table>   

                    </div>
                </div>
                </div>
            </div>
            </div>
        </div>
        <script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.5.1.js"></script>
        <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="mainAJAX.js"></script>
    </body>
</html>

And this is the mainAJAX.js file:

$(document).ready(function() {
  $('#example').DataTable( {
      // "ajax": "http://localhost:2121/arrays.txt"
      "ajax": "https://www.rapidtech1898.com/aaadownload/arrays.txt"
  } );
} );

Somebody told me - that if also the server is hosted on the same domain (like rapidtech1898.com) it would probably work. But is there no way to test such think locally before deploying this somewhere else?

2 Answers 2

1

For reasons of security, that is how CORS works. You cannot request data from a different origin unless the server allows it. However, for the purpose of development, you can disable CORS in multiple ways. There is a nice article for it here. But my personal favourite is this solution (to use a proxy that doesn't bypasses CORS):

fetch('https://cors-anywhere.herokuapp.com/https://www.rapidtech1898.com/aaadownload/arrays.txt', {
    method: "GET",
    headers: {
      "Content-type": "application/json;charset=UTF-8",
      "x-requested-with": "localhost:3000"
    }
  }).then(response => response.json())
  .then(json => console.log(json))
  .catch(err => console.log(err));

Sign up to request clarification or add additional context in comments.

6 Comments

Ok thanks. But why is this not working locally - then both infos are locally? Isn't that then the same origin?
Can't tell what error message you got when you tried it locally. So not sure.
The same error - but only for localhost - see above
What path did you provide for the local file in the ajax field?
The first answer here will help you understand the "why" of it - stackoverflow.com/questions/19842314/…
|
0

I think i found the problem -

In the server-js i have to usse express.static for the data-folder:

const express = require("express");
const app = express();
app.use(express.static('data'))
app.listen(2121, () => {
  console.log("Server is running on port 2121...");
});

And in the datafolder i have to put all files (index.html, mainAJAX.js and arrays.txt).

Then i can call the text-file in the mainAJAX.js like that:

$(document).ready(function() {
  $('#example').DataTable( {
      "ajax": "arrays.txt"
  } );
} );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.