0

I am using nginx web server and running a website on localhost. I want to be able to get my files through the server with jquery's AJAX function but it always throws an error. I've made a folder called TESTFOLDER inside my 'data' folder and it has a txt file called test.txt.

This is my javascript code

$.ajax({
  url: "../data/TESTFOLDER/",
  type: 'GET',
  dataType: "txt",
  headers: { 
    Accept : "text/html",
    "Content-Type": "text/html"
   },
  success: function(data){
    console.log('there was a success');
  },
  error: function (jqXHR, textStatus, errorThrown) {
        console.log('there was an error');
  }
});

This is my nginx configuration block for the folder

    location /data/TESTFOLDER/ {
        autoindex on;
        try_files $uri $uri;
        add_header Content-Type application/txt;
    }

I always get this error:

NetworkError: 500 Internal Server Error

when it tries to reach the data/TESTFOLDER

I have of course tried it simpler than this, without the headers and all that. Nothing seems to work though. Please help if you can.

EDIT:

  • Yes, I can get to the file by typing out local host/data/TESTFOLDER/test.txt in my browser.
  • The ../ doesn't seem to be causing problems because in both cases, I get the error message 500 Internal Server Error - local host/data/TESTFOLDER/, i.e. not local host/../data/TESTFOLDER/
  • The server error log says: 2020#2644: rewrite or internal redirection cycle while internally redirecting to "/data/TESTFOLDER/", client: 127.0.0.1, server: localhost, request: "GET /data/TESTFOLDER/ HTTP/1.1", host: "localhost", referrer: "http:// localhost/index.html"

NOTE: The word local host is in two seperate words here because they won't allow me to post links.

EDIT2: I should note that while local host/data/TESTFOLDER/test.txt works fine in my browser, local host/data/TESTFOLDER/ throws the error 500. Should this be happening?

3 Answers3

0

your url is malformed. if you want to use a relative path in an Ajax call, you don't need to use the ...

source: Relative URLs in AJAX requests

You can just use url: "/data/TESTFOLDER/". Your current url tries to access http://localhost:63542/../Data/TESTFOLDER/, which probably doesn't exist, but the server tries to locate it and throws an error.

Community
  • 1
  • 1
Nzall
  • 3,439
  • 5
  • 29
  • 59
0

You can catch different http-statuscodes with ajax.

Example:

$.ajax({
  statusCode: {
    500: function() {
      // do something
    }
  }
});
Andreas Furster
  • 1,558
  • 1
  • 12
  • 28
0

You misuse try_files and get internal redirection loop.

Also, MIME type for text is text/plain.

Alexey Ten
  • 13,794
  • 6
  • 44
  • 54
  • Yes you are correct in that, I fixed it and now I don't get the 500 server error anymore. It seems that he gets to local host/data/TESTFOLDER/ just fine but ajax still errors? – user3820230 Jul 09 '14 at 13:13