【发布时间】:2019-08-08 09:25:13
【问题描述】:
我正在使用 superagent v3.8.3 我收到Access-Control-Allow-Origin CORS 错误。
import request from 'superagent'
request(
APIS.IAMPORT.GET_TOKEN.method,
APIS.IAMPORT.GET_TOKEN.path()
)
.withCredentials()
.send({
imp_key: config.iamport.apiKey,
imp_secret: config.iamport.secretKey
})
.end((err, res) => {
if (err) reject(err)
else resolve(res.body)
})
我尝试使用.withCredentials(),但它不起作用。
我从 api 收到此错误消息。
apply?id=21:1 从源“http://localhost:8080”访问 XMLHttpRequest 在“https://api.iamport.kr/users/getToken”已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问控制” -Allow-Origin' 标头存在于请求的资源上。
但是,当我使用邮递员时,我可以得到结果。
我认为删除 OPTION http 方法会起作用。但我不确定如何解决这个问题。
如何解决这个问题?谢谢。
【问题讨论】:
-
@sideshowbarker 你的意思是,我不能使用那个 api 调用?
-
您可以从后端代码中使用该 API,但就您的前端代码而言,是的,我的意思是您不能使用该 API 调用——不能从您的前端代码中使用。
-
谢谢,当我在后端代码中调用 request 时它可以工作。
标签: http cors superagent http-method