[英]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: 编辑:
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.