简体   繁体   English

jQuery AJAX总是错误

[英]Jquery AJAX always errors

I am using nginx web server and running a website on localhost. 我正在使用nginx Web服务器并在localhost上运行网站。 I want to be able to get my files through the server with jquery's AJAX function but it always throws an error. 我希望能够使用jquery的AJAX函数通过服务器获取文件,但始终会引发错误。 I've made a folder called TESTFOLDER inside my 'data' folder and it has a txt file called test.txt. 我在“数据”文件夹中创建了一个名为TESTFOLDER的文件夹,并且其中有一个名为test.txt的txt文件。

This is my javascript code 这是我的JavaScript代码

$.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 这是我的文件夹的nginx配置块

    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 当它尝试到达数据/ 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. 是的,我可以通过在浏览器中键入本地主机/数据/TESTFOLDER/test.txt来访问文件。
  • 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/, ie not local host/../data/TESTFOLDER/ ../似乎没有引起问题,因为在两种情况下,我都收到错误消息500 Internal Server Error-本地主机/数据/ TESTFOLDER /,即不是本地主机/../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" 服务器错误日志显示:2020#2644:内部重定向到“ / data / TESTFOLDER /”时,重写或内部重定向周期,客户端:127.0.0.1,服务器:localhost,请求:“ GET / data / TESTFOLDER / HTTP / 1.1” ,主机:“ localhost”,引荐来源网址:“ 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? EDIT2:我应该注意,虽然本地主机/数据/TESTFOLDER/test.txt在我的浏览器中可以正常工作,但本地主机/数据/ TESTFOLDER /引发错误500。这是否会发生?

your url is malformed. 您的网址格式不正确。 if you want to use a relative path in an Ajax call, you don't need to use the .. . 如果要在Ajax调用中使用相对路径,则无需使用..

source: Relative URLs in AJAX requests 来源: AJAX请求中的相对URL

You can just use url: "/data/TESTFOLDER/" . 您可以只使用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. 您当前的URL尝试访问http://localhost:63542/../Data/TESTFOLDER/ ,该http://localhost:63542/../Data/TESTFOLDER/可能不存在,但是服务器试图找到它并引发错误。

You can catch different http-statuscodes with ajax. 您可以使用ajax捕获不同的http状态代码。

Example: 例:

$.ajax({
  statusCode: {
    500: function() {
      // do something
    }
  }
});

You misuse try_files and get internal redirection loop. 您滥用try_files并获得内部重定向循环。

Also, MIME type for text is text/plain . 另外,文本的MIME类型是text/plain

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM