简体   繁体   English

在JavaScript中使用JSON数据打开远程文件

[英]Opening a remote file with JSON data in javascript

I am trying to access a simple data file via javascript and finding it incredibly difficult. 我试图通过javascript访问一个简单的数据文件,发现它异常困难。 It will be placed on a remote server and hopefully could be accessed via http. 它将放置在远程服务器上,希望可以通过http访问。 I am new to javascript, but here goes: the best option so far I have found in JSONP, as stated here https://learn.jquery.com/ajax/working-with-jsonp/ . 我是javascript新手,但这里有:到目前为止,我在JSONP中找到了最好的选择,如此处https://learn.jquery.com/ajax/working-with-jsonp/所述

I have created an example datafile that I would have to process: http://murded.keeleleek.ee/test2.txt . 我创建了一个示例数据文件,需要处理: http : //murded.keeleleek.ee/test2.txt

<html>

<head>
    <title>My experiment</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
    // Using YQL and JSONP
    $.ajax({
        url: "http://murded.keeleleek.ee/test2.txt",

        // The name of the callback parameter, as specified by the YQL service
        //jsonp: "callback",

        // Tell jQuery we're expecting JSONP
        dataType: "jsonp",

        // Tell YQL what we want and that we want JSON
        data: {
            q: "dataprev",
            format: "json"
        },

        // Work with the response
        success: function(response) {
            console.log(response); // server response
        }
    });
</script>

</html>

Eventually I would just like to use the JSON values as an array, and for now, just print them out to console or screen to make sure they are there. 最终,我只想将JSON值用作数组,现在,只需将它们打印到控制台或屏幕上以确保它们在那里即可。 Is anyone able to help me troubleshoot this? 有人可以帮助我解决此问题吗? Perhaps there is a much easier way? 也许有一种更简单的方法?

Unfortunately I am new to both javascript and JSON, so there may be an easy newbie error there. 不幸的是,我对javascript和JSON都是陌生的,所以那里可能有一个简单的新手错误。 The file should be on a remote server, and just read access should be enough for import. 该文件应位于远程服务器上,并且只需读取访问权限即可导入。 Many thanks! 非常感谢!

A simple option is using $.getJson 一个简单的选项是使用$ .getJson

$.getJSON(
        "http://murded.keeleleek.ee/test2.txt",               
           data: {
        q: "dataprev",
        format: "json"
    },
function( response ) {
        console.log( response ); // server response
    }
        )

If your code, you said: 如果是您的代码,您说:

// Tell jQuery we're expecting JSONP

But your URL does not contain JSONP. 但是您的网址不包含JSONP。 It contains plain text. 它包含纯文本。

In order to process it as JSONP you must format it as JSONP . 为了将其处理为JSONP,您必须将其格式化为JSONP

That means: 这意味着:

  • A core of JSON (See http://json.org/ for examples) JSON的核心(有关示例,请参见http://json.org/
  • Wrapped with a JavaScript function call: foo(YOUR_JSON); 用JavaScript函数调用包装: foo(YOUR_JSON);
  • Served with an application/javascript content type (so you should use a .js file extension if you are serving up a static file) application/javascript内容类型一起提供(因此,如果要提供静态文件,则应使用.js文件扩展名)

If you are using a static file, then you are forced to hard code the callback name ( foo in the example above). 如果使用的是静态文件,则必须对回叫名称(上例中的foo )进行硬编码。 You need to tell jQuery what that callback name is instead of letting it generate one dynamically: 您需要告诉jQuery该回调名称是什么,而不是让它动态生成一个回调名称:

jsonp: "foo"

NB: JSONP is a hack to work around cross domain issues. 注意:JSONP是可解决跨域问题的黑客。 It imposes quite a few limits compared to using XMLHttpRequest, including restrictions on what you can do to handle errors. 与使用XMLHttpRequest相比,它施加了很多限制,包括对处理错误的限制。 There is now good support for CORS in browsers, so JSONP isn't needed any more. 现在在浏览器中对CORS有了很好的支持,因此不再需要JSONP。 If you aren't making requests across origins then you don't event need that. 如果您不在跨源发出请求,那么您根本不需要它。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM