【发布时间】:2017-02-16 00:39:00
【问题描述】:
我正在尝试将来自 Dropbox 公用文件夹的 JSONP 调用从不同站点上的 HTML 页面中提取出来(因此我需要 jsonp 来避免跨站点问题)。
我的 javascript 代码报错,因为它获取的是 HTML 而不是 JSON。
$.ajax({
url: "https://www.dropbox.com/s/fzqhnr39fq45ijh/gen_info.json?dl=0",
jsonp: "callback",
dataType: "jsonp",
// Work with the response
success: function( response ) {
console.log( response ); // server response
}
});
如果我从 firebug 控制台复制确切的 URL 并使用 wget 尝试,我会得到正确的文件内容。但是,如您所见,发生了重定向。调试窗口的内容表明 ajax 正在尝试显示第一个 URL 的 HTML,而不是遵循重定向
$wget -O try.txt 'https://www.dropbox.com/s/fzqhnr39fq45ijh/gen_info.json?dl=0&callback=jQuery31100535301754706522_1475816277772&_=1475816277773'
--2016-10-07 15:35:25-- https://www.dropbox.com/s/fzqhnr39fq45ijh/gen_info.json?dl=0&callback=jQuery31100535301754706522_1475816277772&_=1475816277773
Resolving www.dropbox.com... 108.160.172.238
Connecting to www.dropbox.com|108.160.172.238|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.dropboxusercontent.com/content_link/OCqme2gZH9FE2dTWU3DATOHjkk3V7PpjPZZAp6N6hK7cztzhiRTflEh27aFDA8bK/file [following]
--2016-10-07 15:35:26-- https://dl.dropboxusercontent.com/content_link/OCqme2gZH9FE2dTWU3DATOHjkk3V7PpjPZZAp6N6hK7cztzhiRTflEh27aFDA8bK/file
Resolving dl.dropboxusercontent.com... 45.58.69.5
Connecting to dl.dropboxusercontent.com|45.58.69.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2698 (2.6K) [text/plain]
Saving to: `try.txt'
100%[======================================================================>] 2,698 --.-K/s in 0s
2016-10-07 15:35:28 (426 MB/s) - `try.txt' saved [2698/2698]
我遵循了dropbox jsonp file 的建议,并像这样预先包装了 json,但 ajax 调用永远不会到达该页面。
callback( {"HALLWF1": ["2016/10/07 15:05:00", "Hallett 1 Wind Farm", "SA1", "Wind", "-33.300303", "138.726975", 77.5, 94.5] )
有什么方法可以强制 ajax 遵循 Dropbox 重定向,还是我应该放弃并为 json 选择另一个主机?浏览器是 firefox 45.2.0,如果有区别的话。
也在最新的chrome中测试过,有错误
Refused to execute script from 'https://www.dropbox.com/s/fzqhnr39fq45ijh/gen_info.json?dl=0&callback=jQuery311040161567467680626_1475817828807&_=1475817828808' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
【问题讨论】:
标签: ajax jsonp dropbox-api