根据jsonlint,我似乎有一个有效的JSON对象:
{"data":[{"name":"undefined - blablabla (undefined)"},{"name":"undefined - blablabla (undefined)"},{"name":"undefined - blablabla (undefined)"},{"name":"undefined - On blablabla (undefined)"}]}
但是,我遇到以下错误:
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
这是我的反应成分:
componentDidMount() {
fetch('/', {
method: 'GET',
withCredentials: true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json' },
credentials: 'same-origin' })
.then(res => res.json())
.then(user => this.setState({ data: data }));
}
这是我从服务器端发送响应的方式:
app.get(
"/auth/google/callback",
passport.authenticate("google", { failureRedirect: "/error", session: false }),
function(req, res) {
var token = req.user.token;
request('https://www.googleapis.com/analytics/v3/management/accounts?access_token=' + token,
function (error, response, body) {
let views = []
JSON.parse(body).items.forEach(view => {
views.push({
name: view.webPropertyId + ' - ' + view.name + ' (' + view.websiteUrl + ')'
})
})
console.log(JSON.parse(body).items);
res.json({data:views});
});
}
);
EDIT#1:我用'Accept': 'application/json'