dropbox jsonp文件 [英] dropbox jsonp file

查看:84
本文介绍了dropbox jsonp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用来自跨域的纯javascript / html下载一些数据,特定于Dropbox。

I'm trying to download some data using pure javascript/html from cross-domain, dropbox to be specific.

<html>
<head>
</head>
<body>
    <div id = 'twitterFeed'></div>
    <script>
    function myCallback(dataWeGotViaJsonp){
        var text = '';
        var len = dataWeGotViaJsonp.length;
        for(var i=0;i<len;i++){
            twitterEntry = dataWeGotViaJsonp[i];
            text += '<p><img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>'
        }
        document.getElementById('twitterFeed').innerHTML = text;
    }
    </script>
    <script type="text/javascript" src="http://dl.dropbox.com/u/6438697/padraicb.json?count=10&callback=myCallback"></script>
</body>

出于某种原因,json没有加载。但是,当我制作网址时,json正确加载 http:/ /twitter.com/status/user_timeline/padraicb.json?count=10&callback=myCallback 而不是。我从这里获得了这个例子。

for some reason, the json is not loading. however the json loads correctly when I make the url "http://twitter.com/status/user_timeline/padraicb.json?count=10&callback=myCallback" instead. I got this example from here

有人可以解释为什么dropbox无效吗?

Can anybody explain why dropbox doesn't work?

谢谢!

更新:

<script type=text/javascript>
function myCallback(dataWeGotViaJsonp){
    alert(dataWeGotViaJsonp);
}
</script>
<script type="text/javascript" src="http://dl.dropbox.com/u/6438697/test2?&callback=myCallback"></script>

返回[object object]或undefined ......还有什么问题? test.json的内容是myCallback({你的:json});

returns either [object object] or undefined... something is still wrong? the contents of test.json is myCallback( {"your":"json"} );

推荐答案

你不能只是将回调一词添加到您的网址,并希望Dropbox将其包装为JSONP。您将一个JSON文件放在Dropbox上并公开共享,但Dropbox不是动态服务器。您需要一个可编写脚本的环境来获取回调参数值并将其包装在JSON周围以使其成为JSONP。

You can't just add the word 'callback' to your URL and expect Dropbox to wrap it for JSONP. You put a JSON file on your Dropbox and shared it publicly, but Dropbox isn't a dynamic server. You need a scriptable environment to take the callback parameter value and wrap it around the JSON in order to make it "JSONP".

Twitter URL工作的原因是他们的API被配置为将回调参数作为客户端期望JSONP的标志,这实际上只是包含在回调函数中的JavaScript对象文字的一个奇特术语。你告诉twitter将调用该函数,它们将返回一个浏览器将作为脚本执行的文件,将该对象作为参数传递给你的回调函数。

The reason the Twitter URL works is that their API is configured to take the callback parameter as a sign that the client is expecting JSONP, which is really just a fancy term for "a JavaScript object literal wrapped in a callback function". You tell twitter what that function will be called, and they'll return a file that the browser will execute as a script, passing the object as a parameter to your callback function.

如果您不需要JSONP回调函数名称是动态的,您需要使用Dropbox,只需自己包装JSON即可。您需要做的就是编辑文件,并在函数名前加上有效的JSON,然后用紧密的paren附加它。

If you don't need the JSONP callback function name to be dynamic AND you need to use Dropbox, just wrap the JSON yourself. All you need to do is edit the file, and prepend valid JSON with the name of the function, and append it with the close paren.

ie,

myCallback(  {"your":"json"}  );

这篇关于dropbox jsonp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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