我将Contentful作为CMS集成到我的react web应用程序中。npm包Contentful允许访问使用createClient
方法存储的内容。该方法使用Axios创建对内容分发API的请求。
client = contentful.createClient({
space: *** ,
accessToken: ***
})
// getting data
fetchPosts = () => this.client.getEntries()
但问题是-我已经在我的应用程序中使用Axios,并且通过我的react客户端发起的每个请求都包含默认的通用头auth-token
。由于这一点,我得到以下错误为我的HTTP请求-Request header field auth-token is not allowed by Access-Control-Allow-Headers in preflight response.
我尝试将整个头设置为空对象,并将auth-token
设置为未定义但是它没有帮助,因为我的请求仍然包含我的默认头的键-
client = contentful.createClient({
space: *** ,
accessToken: ***,
headers: {}
})
// OR
client = contentful.createClient({
space: *** ,
accessToken: ***,
headers: { 'auth-token' : undefined }
})
我遇到了这篇文章,它回答了如何修改一个请求,不包括一个共同的标题,但我不确定如何使用它来修改createClient
方法。谢谢你的帮助。
在不使用contentful的javascript SDK的情况下尝试了以下内容,它可以工作:
- 为特定请求修改Axios实例以删除默认头
- 使用基础(+普通)url访问内容交付API。
const url =
`${baseUrl}/spaces/${spaceId}/environments/master/entries?access_token=${accessToken}` ;
axios.get(url, {
transformRequest: (data, headers) => {
delete headers.common['auth-token'];
return data;
}
})