Jsonstub 响应未显示

Jsonstub response not showing

axios.get('http://jsonstub.com', {
  headers: {
    'Content-Type': 'application/json',
    'JsonStub-User-Key': '1699b7fc-cdcb-4205-b30a-b9b3626246c5',
    'JsonStub-Project-Key': 'f39f1bd2-d303-4b7e-9a11-c0f049a46f87'
}
}).then((data) => console.log(data))

我已经设置了 json存根,但是当我尝试使用 axios 发出请求时,我得到状态 200 OK,并有 html 响应。当我打开 html 时,它显示 "Page not found"。用 Postman 做这件事让我恢复了我的 json。我从 localhost:8080

开始

这是html的内容: “

<!DOCTYPE html>
         <html>
         <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Jsonstub</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <base href="/" />
<meta name="jsonstub-ember/config/environment" content="%7B%22modulePrefix%22%3A%22jsonstub-ember%22%2C%22environment%22%3A%22production%22%2C%22baseURL%22%3A%22/%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%7D%2C%22APP%22%3A%7B%22API%22%3A%7B%22devMode%22%3Afalse%2C%22host%22%3A%22https%3A//jsonstub.com%22%2C%22namespace%22%3A%22api%22%7D%2C%22STRIPE%22%3A%7B%22pubKey%22%3A%22pk_B95JCBxoUTpYXMGMKuPj0TTKDq30k%22%7D%7D%2C%22simple-auth%22%3A%7B%22authorizer%22%3A%22simple-auth-authorizer%3Aoauth2-bearer%22%2C%22crossOriginWhitelist%22%3A%5B%22http%3A//jsonstub.dev%22%2C%22http%3A//jsonstub.com%22%5D%2C%22routeAfterAuthentication%22%3A%22projects%22%2C%22store%22%3A%22simple-auth-session-store%3Alocal-storage%22%7D%2C%22simple-auth-oauth2%22%3A%7B%22serverTokenEndpoint%22%3A%22https%3A//jsonstub.com/oauth/v2/token%22%2C%22serverTokenRevocationEndpoint%22%3A%22https%3A//jsonstub.com/oauth/v2/token/destroy%22%2C%22refreshAccessTokens%22%3Atrue%7D%2C%22contentSecurityPolicyHeader%22%3A%22Content-Security-Policy-Report-Only%22%2C%22contentSecurityPolicy%22%3A%7B%22default-src%22%3A%22%27none%27%22%2C%22script-src%22%3A%22%27self%27%22%2C%22font-src%22%3A%22%27self%27%22%2C%22connect-src%22%3A%22%27self%27%22%2C%22img-src%22%3A%22%27self%27%22%2C%22style-src%22%3A%22%27self%27%22%2C%22media-src%22%3A%22%27self%27%22%7D%2C%22exportApplicationGlobal%22%3Afalse%7D" />

        <link rel="stylesheet" href="assets/vendor-ec593555806ed9257ba3499907f73f05.css">
        <link rel="stylesheet" href="assets/jsonstub-ember-11fc6f9189ee15ceb15648c1eb7cfe98.css">
        <script>
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-47943224-1', 'auto');
        </script>
    </head>
    <body>
        <script src="assets/vendor-8d0596b15526f36ae70009098f6d3f25.js"></script>
        <script src="assets/jsonstub-ember-d11e3620aa442db7b2ba53d465f4e89e.js"></script>
    </body>
</html>

可能是 CORS 的问题?你能看看 DevTools 控制台输出吗?

HTML 回复的内容也可能有帮助...

这是因为 header 'Content-Type': 'application/json' 没有在 HTTP 请求中发送。

在 axios 中,如果请求 body 为空,Content-Type 将从 HTTP headers 中删除。反正Content-Type就是请求的类型body,所以这样设计是有道理的。参考GitHub中的discussion

但是,Jsonstub announced 对于任何存根请求,Content-Type: application/json header 必须始终存在。这就是为什么当缺少 header 'Content-Type': 'application/json' 时您会得到奇怪的 html 响应。

解决方法很简单,在 HTTP 请求中传递任意数据 body:

axios.get('http://jsonstub.com', {
  headers: {
    'Content-Type': 'application/json',
    'JsonStub-User-Key': '1699b7fc-cdcb-4205-b30a-b9b3626246c5',
    'JsonStub-Project-Key': 'f39f1bd2-d303-4b7e-9a11-c0f049a46f87'
  },
  data: {}
}).then((data) => console.log(data))