繁体   English   中英

Google Apps爸爸的笑话脚本-JSON解析错误“意外令牌:<”

[英]Google Apps Dad Joke Script - JSON parse error “Unexpected token: < ”

我正在尝试使用此GAS JSON指南爸爸笑话API中拉一个笑话,但是当我尝试获取JSON对象时却遇到了解析错误。

当我调用JSON.parse(dadJokeText);时发生错误JSON.parse(dadJokeText);

我尝试的第一个修复方法是发布我的应用程序以供公共,匿名使用 (可能是auth问题),但没有成功。

尝试使用此建议重新格式化我的代码,仍然是同样的错误。

我没有使用JSON的任何经验,很抱歉,这是一篇重复的文章,尝试从其他问题中学习还没有成功。

提前致谢

function getJoke() {
  var url = "https://icanhazdadjoke.com/";
  var options = {
    "contentType" : "application/json" 
  }

  // make a GET request to the dad joke API
  var dadJokeJSON = UrlFetchApp.fetch(url);
  var dadJokeText = dadJokeJSON.getContentText();
  var dadJokeObject = JSON.parse(dadJokeText);
  var joke = dadJokeObject.value['joke'];



  // log the joke that you retrieved
  Logger.log(joke);

}

编辑:这是dadJokeText的值,看起来像页面的HTML

[18-12-10 12:06:11:690 EST] <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<meta name="description" content="The largest collection of dad jokes on the internet" />
<meta name="author" content="C653 Labs" />
<meta name="keywords" content="dad,joke,funny,slack,alexa" />
<meta property="og:site_name" content="icanhazdadjoke" />
<meta property="og:title" content="icanhazdadjoke" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://icanhazdadjoke.com/j/7UnjNRfapzd" />
<meta property="og:description" content="Why should you never trust a pig with a secret? Because it's bound to squeal." />
<meta property="og:image:url" content="https://icanhazdadjoke.com/j/7UnjNRfapzd.png" />
<meta property="og:image:secure_url" content="https://icanhazdadjoke.com/j/7UnjNRfapzd.png" />
<meta property="og:image:secure_url" content="https://icanhazdadjoke.com/static/smile.png" />
<meta property="og:image:type" content="image/png" />
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@icanhazdadjoke">
<meta name="twitter:title" content="icanhzdadjoke">
<meta name="twitter:description" content="Why should you never trust a pig with a secret? Because it's bound to squeal.">
<meta name="twitter:image" content="https://icanhazdadjoke.com/static/smile.png">
<meta name="twitter:url" content="https://icanhazdadjoke.com/j/7UnjNRfapzd" />
<link rel="canonical" href="https://icanhazdadjoke.com/j/7UnjNRfapzd">
<link rel="amphtml" href="https://icanhazdadjoke.com/j/7UnjNRfapzd/amp">
<title>icanhazdadjoke</title>
<link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png">
<link rel="icon" type="image/png" href="/static/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/static/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/static/manifest.json">
<link rel="mask-icon" href="/static/safari-pinned-tab.svg" color="#1fc8db">
<link rel="shortcut icon" href="/static/favicon.ico">
<meta name="msapplication-config" content="/static/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" href="/static/dist/style.css?55f4ea12">
<script type="application/ld+json">
      {
        "@context": "http://schema.org",
        "@type": "WebSite",
        "url": "https://icanhazdadjoke.com/",
        "name": "icanhazdadjoke",
        "description": "The largest collection of dad jokes on the internet",
        "potentialAction": {
          "@type": "SearchAction",
          "target": "https://icanhazdadjoke.com/search?term={search_term_string}",
          "query-input": "required name=search_term_string"
        }
      }
    </script>
<script type="application/ld+json">
      {
        "@context": "http://schema.org",
        "@type": "Organization",
        "url": "https://icanhazdadjoke.com/",
        "logo": "https://icanhazdadjoke.com/static/smile.png",
        "name": "icanhazdadjoke",
        "description": "The largest collection of dad jokes on the internet",
        "email": "support@icanhazdadjoke.com"
      }
    </script>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
      (adsbygoogle = window.adsbygoogle || []).push({google_ad_client: "ca-pub-3200991035275362", enable_page_level_ads: true});
    </script>
</head>
<body>
<nav class="nav has-shadow">
<div class="container">
<div class="nav-left">
<a class="nav-item is-brand" href="/">
<img src="/static/smile.svg" alt="icanhazdadjoke logo" />
<span class="subtitle pushhalf--left">
icanhazdadjoke
</span>
</a>
</div>
<span id="nav-toggle" class="nav-toggle">
<span></span>
<span></span>
<span></span>
</span>
<div id="nav-menu" class="nav-right nav-menu">
<a class="nav-item is-tab" href="/">
<span class="icon is-small">
<i class="fa fa-random"></i>
</span>
<span class="pushquarter--left">
Random joke
</span>
</a>
<a class="nav-item is-tab" href="/search">
<span class="icon is-small">
<i class="fa fa-search"></i>
</span>
<span class="pushquarter--left">
Search jokes
</span>
</a>
<a class="nav-item is-tab" href="/submit">
<span class="icon is-small">
<i class="fa fa-pencil-square-o"></i>
</span>
<span class="pushquarter--left">
Submit new joke
</span>
</a>
<div class="nav-item">
<a href="/slack-add" style="height:40px; max-width:139px">
<img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" style="width:139px; height:40px; max-height: 40px" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x" />
</a>
</div>
<div class="nav-item">
<a class="button" target="_blank" href="https://www.amazon.com/Brett-Langdon-icanhazdadjoke/dp/B01N6CQ3NZ/" style="background-color: #ffffff; color: #5ebfe4; border-radius: 6px; font-weight: 500; height:40px; width:139px; max-width:139px">
<img src="/static/alexa-logo.png" style="margin-left: -0.5em;">
Add to <strong style="margin-left: 0.25em">Alexa</strong>
</a>
</div>
</div>
</div>
</nav>
<section class="section">
<div class="container">
<div class="level">
<div class="level-left">
<div class="content level-item">
<h1>Random dad joke:</h1>
</div>
</div>
<div class="level-right">
<a class="button is-primary level-item" href="/">
<span class="icon is-small">
<i class="fa fa-random"></i>
</span>
<span>
New joke
</span>
</a>
</div>
</div>
<div class="content">
<div class="card">
<div class="card-content">
<p class="subtitle">Why should you never trust a pig with a secret? Because it's bound to squeal.</p>
</div>
<footer class="card-footer">
<a class="card-footer-item" href="/j/7UnjNRfapzd">
<span class="icon is-small">
<i class="fa fa-chain"></i>
</span>
<span class="pushquarter--left">
Permalink
</span>
</a>
<a class="card-footer-item" target="_blank" href="https://twitter.com/intent/tweet?url=https://icanhazdadjoke.com%2Fj%2F7UnjNRfapzd%2Famp&text=Check+out+this+%23dadjoke&via=icanhazdadjoke">
<span class="icon is-small">
<i class="fa fa-twitter"></i>
</span>
<span class="pushquarter--left">
Share on Twitter
</span>
</a>
</footer>
</div>
</div>
<div class="content pushwhole--top">
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3200991035275362" data-ad-slot="5324903139" data-ad-format="auto"></ins>
</div>
<script>
          (adsbygoogle = window.adsbygoogle || []).push({});
        </script>
</div>
</section>
<footer class="footer">
<div class="container">
<div class="columns">
<div class="column is-5">
<div class="content">
<p>
<strong>icanhazdadjoke.com</strong> by <a href="https://c653labs.com/">C653 Labs</a>.
</p>
<p>
<i>icanhazdadjoke.com</i> is the largest selection of dad jokes on the internet.
Now supporting many different integrations to ensure you can access the dad jokes that you need wherever you are.
</p>
</div>
</div>
<div class="column is-3"></div>
<div class="column is-2">
<aside class="menu">
<ul class="menu-list">
<li><a href="/cdn-cgi/l/email-protection#94e7e1e4e4fbe6e0d4fdf7f5fafcf5eef0f5f0fefbfff1baf7fbf9" target="_blank">Contact</a></li>
<li><a href="https://twitter.com/icanhazdadjoke" target="_blank">Twitter</a></li>
<li><a href="https://slack.com/apps/A214NCJF2-icanhazdadjoke" target="_blank">Slack app</a></li>
<li><a href="https://www.amazon.com/Brett-Langdon-icanhazdadjoke/dp/B01N6CQ3NZ/" target="_blank">Alexa skill</a></li>
<li><a href="https://discordapp.com/api/oauth2/authorize?client_id=467365247921946626&permissions=0&scope=bot" target="_blank">Discord bot</a></li>
<li><a href="https://marketplace.atlassian.com/plugins/com.icanhazdadjoke/cloud/overview" target="_blank">HipChat plugin</a></li>
<li><a href="https://twistapp.com/integrations/install/20_901fa9d3df5b8784d3ddc56e" target="_blank">Twist integration</a></li>
</ul>
</aside>
</div>
<div class="column is-2">
<aside class="menu">
<ul class="menu-list">
<li><a href="/about">About</a></li>
<li><a href="/api">API</a></li>
<li><a href="/">Random joke</a></li>
<li><a href="/search">Search jokes</a></li>
<li><a href="/submit">Submit new joke</a></li>
</ul>
<

API所需的 “提取”调用中,没有地方将“ Accept”标头设置为“ application / json”,以便获得格式为JSON的响应。

您确实在代码中设置了一个名为options的变量,但据我所知,您从未将它用于任何用途。 在任何情况下,它都会设置错误的标头-必须为“ Accept”,而不是“ ContentType”-实际上,它应该为“ Content-Type”才是正确的,但是即使这样,Content type仍会告诉服务器您是哪种数据类型发送 ,而不是您想返回的那种。

我希望您需要这样发出请求:

var url = "https://icanhazdadjoke.com/";
var options = { 
  method: 'GET',
  headers: {
    "Accept" : "application/json" 
  }
};

// make a GET request to the dad joke API
var dadJokeJSON = UrlFetchApp.fetch(url, options);

请参阅https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch ,以获取有关JavaScript中的Fetch API的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM