我在Chrome上尝试了以下示例:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
</head>
<body>
test this
</body>
<script>
$.ajax({
type: "GET",
url: "English.csv",
dataType: "text",
success: function(data) {
alert("worked");
},
error: function (request, status, error) {
alert(request.responseText);
}
});
</script>
我在控制台看到以下错误:
XMLHttpRequest无法加载file:///C:/temp/local/English.csv。请求的资源上没有'Access-Control-Allow-Origin'头。因此,源'null'不允许访问。
请参见此答案以获取更多详细信息。
如果您只想在开发环境中使其工作,则可以尝试使用以下命令启动Chrome:
chrome.exe --allow-file-access-from-files
另一种选择是尝试:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
</head>
<body>
test this
</body>
<script>
$.ajaxPrefilter( 'script', function( options ) {
options.crossDomain = true;
});
$.ajax({
type: "GET",
url: "English.csv",
dataType: "script",
success: function(data) {
alert("worked");
},
error: function (request, status, error) {
alert(error);
}
});
</script>