【问题标题】:superagent Access-Control-Allow-Origin error [duplicate]superagent Access-Control-Allow-Origin错误[重复]
【发布时间】: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


【解决方案1】:

问题不在于 HTTP 方法。问题是 CORS,并已尝试在另一个答案中解释原因和解决方案。请参考以下链接:

https://stackoverflow.com/a/54867814/6356369

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 2018-12-12
    • 2017-07-13
    • 1970-01-01
    • 2017-06-15
    • 2020-05-27
    • 1970-01-01
    • 2014-03-05
    相关资源
    最近更新 更多