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 ?
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.
<!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>
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”.
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. “