【问题标题】:API get request working in postman but in jquery ajax getting blocked by CORS policy [duplicate]API获取请求在邮递员中工作,但在jquery ajax中被CORS策略阻止[重复]
【发布时间】:2020-08-07 12:36:56
【问题描述】:

AJAX 请求错误

http://localhost' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头

我的密码

var srv_url = 'https://api-domain.com/api/index.php?type=join&meetingID=test01&moderatorPW=mp&fullName=John';
    var auth_token = 'e6a83f2844c9cdd4729b69dc851f96c1c073a23b4f37eb7cdec83bf8c9c7a06d'

    $.ajax({
      url: srv_url,
      type: 'get',
      beforeSend: function(xhr){
        xhr.setRequestHeader("Authorization", auth_token);
      },
      success: function(res){
        console.log(res)
      },
      error: function(e){
        console.log(e)
      }
    })

但在邮递员中工作

如果存在与 CORS 相关的问题,为什么此请求适用 邮递员在同一台机器上。请帮帮我,我在做什么 我的 ajax 代码错误。

【问题讨论】:

    标签: php jquery ajax api cors


    【解决方案1】:

    您需要在服务器端允许跨源访问:

    header("Access-Control-Allow-Origin: *");
    

    你会发现更多细节here

    【讨论】:

    • 好的。但是它是如何在不允许的情况下在邮递员中工作的。
    • 这行得通吗?我认为 1.5 后的 JQuery 会自动执行此操作。
    • @SumanBiswas 因为Postman doesn't care about SOP, it's a dev tool not a browser more details...
    • @ParitoshMahale 谢谢你帮助我。目前,我无法访问后端。
    猜你喜欢
    • 2020-06-13
    • 2022-01-14
    • 2019-11-30
    • 2019-10-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 2019-09-30
    相关资源
    最近更新 更多