【问题标题】:Why is .getJSON function returning MIME type error from API为什么 .getJSON 函数从 API 返回 MIME 类型错误
【发布时间】:2017-10-13 11:29:04
【问题描述】:

我一直在尝试从Forismatic API 调用,但是当我发送它时它返回一个 MIME 类型错误。

JQuery 请求:

$(document).ready(function() {

    $("#quote-button").on("click", function(){
       $.getJSON("https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en", function(json) {
  $(".quote-text").html(JSON.stringify(quoteText));
});

  });
 });

错误信息

拒绝执行脚本 'https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en&jsonp=jQuery32104928506782842177_1494780473751&_=1494780473752' 因为它的 MIME 类型('application/json')是不可执行的,并且 已启用严格的 MIME 类型检查。

我曾考虑添加callback=? 并更改为https 而不是http,但它们没有奏效。

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    根据 API 文档,您需要将格式设置为 "jsonp" 并设置 jsonp 参数:

    $.getJSON("https://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&lang=en&jsonp=?", function(json) {
      console.log(json)
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 2020-10-15
      • 2016-08-18
      • 2018-01-03
      • 2011-06-17
      相关资源
      最近更新 更多