代码之家  ›  专栏  ›  技术社区  ›  Mani Singh

带有密钥和数据的api链接不起作用[重复]

  •  0
  • Mani Singh  · 技术社区  · 4 年前

    Mod注释 :这个问题是关于为什么 XMLHttpRequest / fetch /等等。浏览器上的内容受相同访问策略限制(您会收到提及CORB或CORS的错误),而Postman则不受此限制。这个问题是 关于如何修复“No'Access Control Allow Origin'…”错误。这是关于为什么它们会发生。

    请停止发帖 :


    我正在尝试使用进行授权 JavaScript 通过连接到 RESTful API 内置 Flask 。但是,当我发出请求时,我会收到以下错误:

    XMLHttpRequest cannot load http://myApiUrl/login. 
    No 'Access-Control-Allow-Origin' header is present on the requested resource. 
    Origin 'null' is therefore not allowed access.
    

    我知道API或远程资源必须设置头,但当我通过Chrome扩展发出请求时,为什么它有效 Postman ?

    这是请求代码:

    $.ajax({
      type: 'POST',
      dataType: 'text',
      url: api,
      username: 'user',
      password: 'pass',
      crossDomain: true,
      xhrFields: {
        withCredentials: true,
      },
    })
      .done(function (data) {
        console.log('done');
      })
      .fail(function (xhr, textStatus, errorThrown) {
        alert(xhr.responseText);
        alert(textStatus);
      });
    
    0 回复  |  直到 1 年前