使用 jQuery GET 加载 CSV 文件返回标题但没有数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11409920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading a CSV file using jQuery GET returns the header but no data
提问by Cees Meijer
When reading a CSV file from a server using the jQuery 'GET' function I do not get any data. When I look at the code using FireBug I can see the GET request is sent and the return value is '200 OK'. Also I see that the header is returned correctly so the request is definitely made, and data is returned. This is also what I see in Wireshark. Here I see the complete contents of the CSV file is returned as a standard HTTP response.
But the actual data is not there in my script. Firebug shows an empty response and the 'success' function is never called. What could be wrong ?
使用 jQuery 'GET' 函数从服务器读取 CSV 文件时,我没有得到任何数据。当我查看使用 FireBug 的代码时,我可以看到发送了 GET 请求并且返回值为“200 OK”。我还看到标头正确返回,因此肯定会发出请求,并返回数据。这也是我在 Wireshark 中看到的。在这里,我看到 CSV 文件的完整内容作为标准 HTTP 响应返回。
但实际数据不在我的脚本中。Firebug 显示一个空响应,并且永远不会调用“成功”函数。有什么问题?
EDIT: An essential part of information appeared to be missing from my question. The following code runs on my local machine and is started by Aptana Studio in Firefox using the built-in test server.
编辑:我的问题中似乎缺少信息的重要部分。以下代码在我的本地机器上运行,并由 Firefox 中的 Aptana Studio 使用内置测试服务器启动。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>New Web Project</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var csvData;
$(document).ready(function() {
$("#btnGET").click(function() {
csvData = $.ajax({
type: "GET",
url: "http://www.mywebsite.com/data/sample_file.csv",
dataType: "text/csv",
success: function () {
alert("done!"+ csvData.getAllResponseHeaders())
}
});
});
})
</script>
</head>
<body>
<h1>New Web Project Page</h1>
<button id="btnGET">GET Data</button>
</body>
</html>
采纳答案by Cees Meijer
The code does not work because I try to make a cross-domain GET. The page and script are hosted on my local machine and the GET tries to retrieve data from a totally different domain. Though technically possible it is blocked by all modern browsers because it is a security vulnerability.
Frederick Behrends pointed me in the right direction. And as he also mentions the only cross domain GET that is allowed is by using "jsonp".
该代码不起作用,因为我尝试进行跨域 GET。页面和脚本托管在我的本地机器上,GET 尝试从完全不同的域中检索数据。尽管在技术上是可行的,但它被所有现代浏览器阻止,因为它是一个安全漏洞。
Frederick Behrends 为我指明了正确的方向。正如他还提到的,唯一允许的跨域 GET 是使用“jsonp”。
Following text was taken from the jQuery documentation:
"Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol. "
以下文本摘自 jQuery 文档:
“由于浏览器安全限制,大多数“Ajax”请求都遵循同源策略;该请求无法从不同的域、子域、端口或协议成功检索数据。”
回答by Frederick Behrends
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>New Web Project</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var csvData;
$(document).ready(function() {
$("#btnGET").click(function() {
csvData = $.ajax({
type: "GET",
url: "http://www.mywebsite.com/data/sample_file.csv",
dataType: "text/csv",
success: function (result) {
alert(result);
alert("done!"+ csvData.getAllResponseHeaders())
}
});
});
})
</script>
</head>
<body>
<h1>New Web Project Page</h1>
<button id="btnGET">GET Data</button>
</body>
</html>
回答by Tim
As my understanding dataType only accepts "text" as parameter, may be you can have a go? You can refer to following page as well: http://api.jquery.com/jQuery.ajax/
由于我的理解 dataType 只接受“text”作为参数,你可以试一试吗?您也可以参考以下页面:http: //api.jquery.com/jQuery.ajax/
Thanks.
谢谢。
回答by djmati11
$.ajax({
type: "GET",
url: "http://www.mywebsite.com/data/sample_file.csv",
dataType: "text/csv",
success: function (data) {
csvData = data;
//alert("done!"+ csvData.getAllResponseHeaders()) - my fix makes this won't work...
}
});
回答by Edgar Wanjala
I tried commenting out dataType and it has worked.
我尝试注释掉 dataType 并且它起作用了。