Axios获取请求成功,但数据属性为空?

时间:2019-06-09 22:48:56

标签: javascript node.js http axios httpresponse

我有一个简单的react组件:

import React from "react"
import axios from "axios"

export default () => {
  const axios = require("axios")

  axios.get("http://localhost:8000/user").then(function(res) {
    // handle success
    console.log(res)
  })

  return (
    <div>
        Hello
    </div>
  )
}

我确实得到了一些返回,状态码为200,它确实显示console.log,但是数据字段为空。只是data: "";我发出的get请求是到我自己的nodejs服务器的。我应该找回当前登录的用户。当我直接访问URL(localhost:8000/user)时,此方法有效。看起来像这样

"displayName": "thomas",
"id": "google-oauth2|197913719739127391723",
"user_id": "google-oauth2|197913719739127391723",
"name": {
"givenName": "thomas3000"
},
"emails": [
{
"value": "thomas3k@tmail.com"
}
],
"picture": "https://akfaf/afaf/photo.jpg",
"locale": "en",
"nickname": "thomas3000",
"_json": {
"sub": "google-oauth2|197913719739127391723",
"given_name": "thomas3k",
"nickname": "tom",
//... and so on; changed the values here.

但是,这是axios响应的console.log: Response seems ok

数据属性是一个空字符串。我不知道为什么我不能在axios get请求中获取此数据?

编辑:(请求后端代码)

后端

//imports here, then:    
passport.use(
  new Auth0Strategy(
    {
      domain: "bla.eu.auth0.com",
      clientID: "afjgnrtkngewmofmwlefmlwems",
      clientSecret:
        "jngnsknkankjangkjangjknKJJKGKAHBJVvgjvVgjVbhJBjhbJbj",
      callbackURL: "http://localhost:8000/callback",
      audience: "https://bla.eu.auth0.com/userinfo",
      responseType: "code",
      scope: "openid email profile",
    },
    function(accessToken, refreshToken, extraParam, profile, done) {
      return done(null, profile)
    }
  )
)

passport.serializeUser(function(user, done) {
  done(null, user)
})

passport.deserializeUser(function(user, done) {
  done(null, user)
})

const app = express()
app.use(cors({ origin: "http://localhost:8001" }))

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.use(
  session({ secret: "secret_key", resave: true, saveUninitialized: true })
)

app.use(passport.initialize())
app.use(passport.session())

app.use(function(req, res, next) {
  res.locals.loggeedIn = false
  if (req.session.passport && typeof req.session.passport.user !== undefined) {
    res.locals.loggedIn = true
  }
  next()
})

app.get("/", function(req, res, next) {
  res.send("Root")
})

app.get(
  "/login",
  passport.authenticate(
    "auth0",
    {
      domain: "bla.auth0.com",
      clientID: "kafmkafmkamfkamfka",
      redirectUri: "http://localhost:8000/callback",
      audience: "https://bla.auth0.com/userinfo",
      responseType: "code",
      scope: "openid email profile",
    },
    function(req, res, next) {
      res.redirect("/")
    }
  )
)

app.get("/logout", function(req, res, next) {
  req.logout()
  res.redirect("/")
})

app.get(
  "/callback",
  passport.authenticate("auth0", {
    failureRedirect: "/failure",
  }),
  function(req, res) {
    res.redirect("/user")
  }
)

app.get("/user", function(req, res, next) {
  res.send(req.user)
})

app.get("/failure", function(req, res, next) {
  res.send("Failure")
})

app.listen(8000, function() {
  console.log("running")
})

0 个答案:

没有答案