Jsonstub的反应没有显示出来 [英] Jsonstub response not showing

查看:215
本文介绍了Jsonstub的反应没有显示出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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))

我已经设置了jsonstub,但是当我尝试使用axios发出请求时,我得到状态200 OK,有html响应。当我打开那个HTML时,它会显示找不到页面。用邮差做这件事让我回到了我的json。我是从localhost做的:8080

I have set up jsonstub, but when I try making a request with axios, I get status 200 OK, with html response. When I open that html it shows me "Page not found". Doing this with Postman gives me back my json. I'm doing this from localhost:8080

这是html的内容:

This is content of 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>


推荐答案

这是因为标题'在HTTP请求中不发送Content-Type':'application / json'

在axios中,如果请求正文为空,<将从HTTP标头中删除code> Content-Type 。无论如何, Content-Type 是请求体的类型,所以这个设计是有意义的。请参阅GitHub中的讨论

In axios, if the request body is empty, Content-Type would be removed from HTTP headers. Anyway, Content-Type is the type of request body, so this design makes sense. Refer to discussion in GitHub.

然而,Jsonstub 宣布 Content-Type:application / json 标头。这就是为什么当标题'Content-Type':'application / json'缺失时你得到奇怪的html响应。

However, Jsonstub announced that the Content-Type: application/json header must always be present for any stubbed requests. That's why you get strange html response when header 'Content-Type': 'application/json' is missing.

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

The workaround is simple, pass arbitrary data in HTTP request 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))

这篇关于Jsonstub的反应没有显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆