允许其他页面访问我的 Tumblr 博客内容 [英] Allow my Tumblr blog's content to be accessed by another page

查看:41
本文介绍了允许其他页面访问我的 Tumblr 博客内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我在其他网页上编写的脚本从我的 Tumblr 博客中复制所有实际内容,但在访问这些内容时遇到了一些麻烦.我的ajax调用如下:

I'm attempting to copy all of the actual content from my Tumblr blog using a script I wrote on a different web page, but I'm having a bit of trouble with gaining access to the content. My ajax call is as follows:

$.ajax({
     url: "http://solacingsavant.tumblr.com/",
     dataType: 'jsonp',
     success: function(data) {
          var elements = $("<div>").html(data)[0].getElementsByTagName("ul")[0].getElementsByTagName("li");
          for(var i = 0; i < elements.length; i++) {
               var theText = elements[i].firstChild.nodeValue;
               alert(theText); // Alert if I got something
              // This is where I'll strip the data for the items I want
          }
     }
});

但因为它是控制台给我一个错误资源被解释为脚本但使用 MIME 类型文本/html 传输",其中 我在这里查看 并将我博客 HTML 中相应的 meta 标签更改为 <代码><meta http-equiv="Content-Type" content="application/javascript; charset=utf-8"/> 没有成功

but as it is the console gives me an error of "Resource interpreted as Script but transferred with MIME type text/html" which I looked into here and changed the corresponding meta tag in the HTML of my blog to <meta http-equiv="Content-Type" content="application/javascript; charset=utf-8" /> with no success

我也尝试使用 dataType: 'html'(这对我来说更有意义)但我收到控制台错误Access-Control-Allow-Origin 不允许 Origin"我也研究过并添加了一个元标记我的 Tumblr 博客带有 <meta Access-Control-Allow-Origin="*"/>,但同样没有成功

I also tried using dataType: 'html' (which makes more sense to me) but I was getting a console error of "Origin is not allowed by Access-Control-Allow-Origin" which I also looked into and added a meta tag to my Tumblr blog with <meta Access-Control-Allow-Origin="*" />, but again didn't succeed

这是一个可以使用的 jsFiddle

我的方法是否行不通,因为 Tumblr 整体上不允许更改 Access-Control?如果是这样,我该如何解决这个问题?如果没有,我做错了什么?

Does my approach not work because Tumblr as a whole does not allow changes to Access-Control? If so, how might I work around the issue? If not, what am I doing wrong?

主要编辑(基于 mikedidthis 的有用评论)

如果没有 Tubmlr API,我似乎无法做到这一点,所以我获得了一个 API 密钥,现在可以访问 API 发出的 json 结果.我能够在控制台中使用 API 密钥获取一个 jsonp 对象.我目前的javascript:

It seems that I am not able to do this without a Tubmlr API, so I obtained an API key and now have access to the json results that the API sends out. I am able to get a jsonp object using the API key to in the console. My javascript at the moment:

$.ajax({
    url: "http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/info?api_key=APIkeyGoesHeRe",
    dataType: 'jsonp',
    success: function(results){
        console.log(results); 
        // Get data from posts here
    }
});

这篇SO帖子有助于理解我可以从源头更改我的Tubmlr 页面上的数据并找到有关该站点的基本信息,但不能从单个帖子中获取实际数据.我尝试查看 results 对象,但找不到与帖子相关的任何数据,也无法将结果附加到 jsfiddle.所以我现在的问题是,我可以使用这种方法从单个帖子中复制数据(比如帖子中的书面文本)吗?如果可以,如何复制?如果不能,我应该使用什么其他方法?"

This SO post was helpful in understanding how I can change data on my Tubmlr page from the source and find out basic information about the site, but not about how to obtain actual data from individual posts. I tried looking through the results object and was unable to find any data related to posts, nor was I able to append the results to the jsfiddle. So my questions now are, "Can I copy data (say the written text in a post) from individual posts using this approach? If so, how? If not, what other approach should I use?"

推荐答案

一个非常快速的答案

tumblr API 文档确实涵盖了如何很好地使用 API,但是,为了让您有一个小小的开始,让我们获取您所有的文本帖子.

A really quick answer

The tumblr API documentation really covers using the API well, however, to give you a little start, lets grab all your Text Posts.

首先,您需要为您的任何文本类型的帖子查询 API.

First you need to query the API for any of your post that are of the type Text.

文档说明(http://www.tumblr.com/docs/en/api/v2#posts),我们应该使用以下网址并指定您将设置为texttype:

The documentation states (http://www.tumblr.com/docs/en/api/v2#posts) that we should use the following url and specifying the type which we you will set to text:

api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts[/type]

以下是基于 OP 小提琴的示例.

And below is an example based on the OP fiddle.

$.ajax({
    url: "http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts/text?api_key=XXXXXXX",
    dataType: 'jsonp',
    success: function(data){
        posts = data.response.posts
        $.each(posts, function(i) {
            console.log( posts[i].title, posts[i].body )
        });
    }
});

因此对于 API 的每个查询,我们都会收到一个对象.您将需要过滤此对象以从中获取所需的数据.

So for each query of the API, we will receive back an object. You will need to filter this object to get the data you want from it.

在帖子查询的上下文中,您可以使用 data.response.posts 对象直接获取您的帖子.

In context of the post queries, you can get directly at your posts using data.response.posts object.

要找出每种帖子类型可用的数据,文档中有介绍:http://www.tumblr.com/docs/en/api/v2#text-posts

To find out what data is available for each post type, the documentation has it covered: http://www.tumblr.com/docs/en/api/v2#text-posts

对于每种 Text 帖子类型的内容,您需要遍历 posts 对象,然后获取名为 title<的键的值/code> 和 body.

To the content for each of the Text post types, you need to loop through the posts object and then grab the value for the key named title and body.

示例:http://jsfiddle.net/ZpFwL/

奖励时间通过从 URL 中删除 type,可以获得所有类型的帖子:

Bonus Time It is possible to get posts for all types, by dropping the type from the URL:

http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts/?api_key=XXXXXXX"

请记住,这是一个非常快速的示例,不适用于现实世界.

Remember this is a really, quick example and not for the real world.

这篇关于允许其他页面访问我的 Tumblr 博客内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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