使用jQuery GET加载CSV文件GET返回头但没有数据 [英] Loading a CSV file using jQuery GET returns the header but no data

查看:182
本文介绍了使用jQuery GET加载CSV文件GET返回头但没有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用jQuery'GET'函数从服务器读取CSV文件时,我没有获得任何数据。当我看看使用FireBug的代码,我可以看到GET请求被发送,返回值是'200 OK'。我还看到头正确返回,所以请求是绝对做出,并返回数据。这也是我在Wireshark看到的。在这里,我看到CSV文件的完整内容作为标准HTTP响应返回。

但是实际数据不在我的脚本中。 Firebug显示空响应,并且成功函数从不调用。什么可能是错误?

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 ?

编辑:信息的一个重要部分似乎从我的问题丢失。以下代码在我的本地计算机上运行,​​并由Aptana Studio在Firefox中使用内置的测试服务器启动。

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>


推荐答案

代码不工作,因为我试图做跨域GET。页面和脚本托管在本地计算机上,GET尝试从完全不同的域中检索数据。虽然在技术上可能,它被所有现代浏览器阻止,因为它是一个安全漏洞。

Frederick Behrends指出我的方向正确。而且他还提到只允许使用jsonp的跨域GET。

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".

以下文本取自jQuery文档:

由于浏览器安全限制,大多数Ajax请求受同一来源政策;请求无法成功从不同的域,子域,端口或协议检索数据。

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 GET加载CSV文件GET返回头但没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆