【发布时间】:2022-01-06 22:06:32
【问题描述】:
从我的前端,我尝试使用以下代码从 Shopify 获取具有 Admin API(GraphQL) 的产品:
*我在 Quasar Framework 上使用了“axios”,这是 Headless Commerce
const response = await this.$axios({
url: "https://healthy-food.myshopify.com/admin/api/2022-01/graphql.json",
method: 'POST',
headers: {
"X-Shopify-Access-Token": "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"
},
data: {
"query": `
{
products(first: 5) {
edges {
node {
id
title
}
}
}
}
`
}
});
console.log(response.data);
但我得到了如下所示的错误:
访问 XMLHttpRequest 在 'https://healthy-food.myshopify.com/admin/api/2022-01/graphql.json' 来自 'http://localhost:8080' 已被 CORS 策略阻止:对预检请求的响应不 通过访问控制检查:没有“Access-Control-Allow-Origin”标头 出现在请求的资源上。
接下来,我将标题 "Access-Control-Allow-Origin": "*" 和 "Content-Type": "application/json" 添加为如下图:
const response = await this.$axios({
url: "https://healthy-food.myshopify.com/admin/api/2022-01/graphql.json",
method: 'POST',
headers: {
"X-Shopify-Access-Token": "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",
"Access-Control-Allow-Origin" : "*", // Here
"Content-Type": "application/json" // Here
},
data: {
"query": `
{
products(first: 5) {
edges {
node {
id
title
}
}
}
}
`
}
});
console.log(response.data);
但我仍然遇到同样的错误:
访问 XMLHttpRequest 在 'https://healthy-food.myshopify.com/admin/api/2022-01/graphql.json' 来自 'http://localhost:8080' 已被 CORS 策略阻止:对预检请求的响应不 通过访问控制检查:没有“Access-Control-Allow-Origin”标头 出现在请求的资源上。
有没有办法解决这个错误?
【问题讨论】:
标签: javascript vue.js graphql shopify quasar-framework