【问题标题】:Why is this JSONP url written like this?为什么这个 JSONP url 是这样写的?
【发布时间】:2017-10-13 21:45:55
【问题描述】:

现在我正在尝试从 freecodecamp 进行随机报价生成器挑战。 但是,我在从网站上获取报价时遇到了很多麻烦 http://api.forismatic.com 由于 CORS ...然后我偶然发现了某人的解决方案,但我不明白他为什么在 url 末尾包含查询 jsonp=?

他创造了:

// Random Quote Generator
var url = "http://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=jsonp&lang=en&jsonp=?";

除了最后的jsonp=? 之外,我关注所有内容... 我知道必须有一个回调,我试图用callback=? 替换它 但随后出现错误:

"Refuse to execute script from (data resource) because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled."

【问题讨论】:

    标签: cors jsonp


    【解决方案1】:

    根据http://forismatic.com/en/api/ 的文档:

    jsonp=——回调函数名,仅用于jsonp格式

    所以他们只有一个 jsonp 参数,而其他 API 使用 callback 完全相同的东西。

    所以给定method=getQuote&key=457653&format=jsonp&lang=en&jsonp=?,一个文字“?”是API将作为回调函数名称返回的内容:

    $ curl "http://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=jsonp&lang=en&jsonp=?"
    
    ?({"quoteText":"I have done my best: that is about all the philosophy of living one needs.","quoteAuthor":"Lin-yutang","senderName":"","senderLink":"","quoteLink":"http://forismatic.com/en/475d78775d/"})
    

    换句话说,参数名称“callback”并没有什么神奇之处——没有标准规定必须使用参数名称,因此任何 API 提供者都可以选择使用他们想要的任何名称——正如 forismic.com 选择的那样用于他们的 API。

    【讨论】:

    • 但我想知道编码员是如何知道“?”是回调函数的名称,因为它没有在 forismic 网站上这么说....有一个示例,但带有 diff 函数名称。什么是“$ curl”?我试过google,一个命令行传输数据的工具?
    • 是的 ? 似乎是一个奇怪的函数名称选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2011-05-12
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多