惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
小众软件
小众软件
有赞技术团队
有赞技术团队
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
J
Java Code Geeks
罗磊的独立博客
V
Visual Studio Blog
雷峰网
雷峰网
H
Help Net Security
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
F
Fortinet All Blogs

Fooleap's Blog

渴望理想 | Fooleap's Blog 19 年底一些事 | Fooleap's Blog 藉秋风,跑起来 | Fooleap's Blog 一个人去跑步 | Fooleap's Blog 8 月的跑量仿佛是那暑假的作业 | Fooleap's Blog 三伏天跑步那么难受,为何还要跑? | Fooleap's Blog 解决小程序开发“当前系统代理不是安全代理” | Fooleap's Blog 忘却配速的夏日跑步 | Fooleap's Blog 六月天时“带水”跑步更爽 | Fooleap's Blog 出来混迟早要还的 | Fooleap's Blog 跑步不能当饭吃 | Fooleap's Blog 将京东移动端详情页链接转为 PC 端 | Fooleap's Blog 不是热就是雨 | Fooleap's Blog 这半月,我跑了 11 个 520 | Fooleap's Blog 跑完流汗一时爽,一直流汗一直爽 | Fooleap's Blog 初夏夜跑 | Fooleap's Blog Electron 中打开 QQ 临时会话 | Fooleap's Blog 春节期间的培隆角 | Fooleap's Blog 从春天跑到夏天 | Fooleap's Blog 晨雾中奔跑 | Fooleap's Blog 漫步春雨中 | Fooleap's Blog 跑在木棉花下 | Fooleap's Blog 我在春节依然坚持跑步 | Fooleap's Blog 伴随着日出跑步 | Fooleap's Blog 没有最好,只有更好 | Fooleap's Blog 电子气温计 | Fooleap's Blog 渡亭小学的金凤花 | Fooleap's Blog 随心而跑 | Fooleap's Blog 2018 跑步小结 | Fooleap's Blog 在 gVim 中使用“非等宽字体” | Fooleap's Blog
Disqus 的 URL 编码问题 | Fooleap's Blog
fooleap · 2017-09-20 · via Fooleap's Blog

编码问题一直是编程界老生常谈的问题。由于鄙人博文不使用特殊字符,因此在使用 Disqus API 时,并没有遇到 URL 编码[1]问题,一切正常。如若博文链接包含汉字,Disqus 又是怎么处理的呢?

我尝试新创建一篇博文来测试,博文链接为 http://blog.fooleap.org/测试链接.html

原生评论框

使用 Disqus 原生评论框,并采用以下配置:

disqus_config = function () {
    this.page.identifier = location.pathname;
    this.page.title = "测试文章";
    this.page.url = location.href;
}

初始化之后,thread 正常创建,返回所设置的三个属性正常。

{
    "identifiers":[
        "/%E6%B5%8B%E8%AF%95%E9%93%BE%E6%8E%A5.html"
    ],
    "link":"http://blog.fooleap.org/%E6%B5%8B%E8%AF%95%E9%93%BE%E6%8E%A5.html",
    "title":"\u6d4b\u8bd5\u6587\u7ae0"
}

中文链接获取 thread 信息

当我以链接去请求 Disqus API 以获取 thread 信息,却返回失败。

var xhr = new XMLHttpRequest();
xhr.open('GET','https://disqus.com/api/3.0/threads/details.json?forum=fooleap&thread:link=http://blog.fooleap.org/%E6%B5%8B%E8%AF%95%E9%93%BE%E6%8E%A5.html&api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F');
xhr.send();

返回的内容,目测链接传过去是被疑似 decodeURI 了。

{
    "code":2,
    "response":"Invalid argument, 'thread': Unable to find thread 'link:http://blog.fooleap.org/\u6d4b\u8bd5\u94fe\u63a5.html'"
}

于是先自我 encodeURI,返回正常。

var xhr = new XMLHttpRequest();
xhr.open('GET','https://disqus.com/api/3.0/threads/details.json?forum=fooleap&thread:link='+encodeURI('http://blog.fooleap.org/%E6%B5%8B%E8%AF%95%E9%93%BE%E6%8E%A5.html')+'&api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F');
xhr.send();

上面是使用页面链接作为参数,请求 Disqus API 获取数据例子。Disqus API 除了可以获取 thread 数据,还可以进行创建 thread 操作,也就是使用原生评论框时,自动创建的那个操作。

当我以等同上面的 identifiertitleurlencodeURIComponent 后作为参数请求 threads/createthread 成功创建,属性和原生评论框创建的一致。

从以上例子可得出,我们在请求 Disqus API 时,只要先把链接 encodeURI 便可。

当然上面例子只是简单测试一下中文,若链接包含一些特殊符号,估计可能又是另一种情况。在这里,我也不再进行深究,我始终认为链接里不包含特殊字符及汉字较好。

另外,threadslug 官方的说明是限制字母及数字,我使用过程中发现 - 符号也行,若不是强迫症,也可以留空。

参考资料

本文历史

  • 2017 年 09 月 20 日 完成初稿

最近更新

猜你喜欢