【问题标题】:403 Forbidden: Able to open uri in browser but not in JavaScript code403 禁止:可以在浏览器中打开 uri,但不能在 JavaScript 代码中打开
【发布时间】:2016-08-01 16:53:43
【问题描述】:

我正在尝试在我的 JavaScript 代码中打开一个(跨域)uri。如果我将它复制到我的浏览器并直接打开它,我就可以打开它。但是,如果我在我的 JavaScript 代码中打开它,我会收到 403 Forbidden 错误。这是我的代码:

var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.withCredentias = true;
xhr.open('get', uri, 'true');
xhr.onload = function() {
    console.log(xhr.response);
}
xhr.send();

这似乎不是 CORS 错误,因为在响应标头中,我可以在 Access-Control-Allow-Origin 部分看到我的源主机。我也没有收到任何 CORS 错误。

有人知道为什么会这样吗?非常感谢您的帮助!

谢谢!

====更新====

直接从浏览器(Chrome)

Request:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:host.com
Pragma:no-cache
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36

Response:
Cache-Control:max-age=60
Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Mon, 01 Aug 2016 17:58:20 GMT
Expires:Mon, 01 Aug 2016 17:59:20 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.6 
Transfer-Encoding:chunked
Vary:Accept-Encoding

来自代码

Request:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:host.com
Origin:myhost.com
Pragma:no-cache
Referer:myhost.com/login
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36

Response:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods:GET, HEAD, OPTIONS
Access-Control-Allow-Origin:myhost.com
Cache-Control:max-age=60
Connection:Keep-Alive
Content-Length:0
Content-Type:application/json; charset=utf-8
Date:Mon, 01 Aug 2016 18:20:54 GMT
Expires:Mon, 01 Aug 2016 18:21:54 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.6

【问题讨论】:

  • 您可以在手动调用 url 和使用 ajax 请求时添加请求/响应标头吗?另外,xhr.withCredentias 你的问题是错字吗?应该是xhr.withCredentials
  • 是的 withCredentials 是一个错字,在我的代码中是正确的。
  • 我稍后会粘贴标题。谢谢
  • 曾经解决过这个问题吗?我在使用 electronjs 时遇到了同样的问题......当我从浏览器提交 GET 请求时工作正常,但从 Javascript 完成时给我 403 错误。

标签: javascript html web xmlhttprequest http-status-code-403


【解决方案1】:

当我将 User-Agent 请求标头更改为 chrome 的标准标头时,我能够解决同样的问题。

【讨论】:

    【解决方案2】:

    我遇到了一个类似的问题,我可以从浏览器向 URL 提交一个 GET 请求,但是使用 JavaScript(electron、node.js、net.request)给我一个 403 错误,其中包含某些数据相同的网址。

    我结束查看浏览器发送的标头,然后将这些相同的标头添加到 Javascript 请求中,然后问题就消失了。

    所以这个答案不是纯 Javascript 答案,但它适用于 Electron javascript。我在 main.js 中添加了这个:

    const electron = require('electron');
     const { net, session } = electron;
    
    
      session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
        details.requestHeaders['Upgrade-Insecure-Requests'] = '1';
        details.requestHeaders['Dnt'] = '1';
        details.requestHeaders['Accept-Encoding'] = 'gzip, deflate, br';
        details.requestHeaders['Accept-Language'] = 'en-US,en;q=0.5';
        details.requestHeaders['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
        details.requestHeaders['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0';
        details.requestHeaders['Connection'] = 'close';
        details.requestHeaders['X-Forwarded-Proto'] = 'http';
        callback({ cancel: false, requestHeaders: details.requestHeaders });
      });
    
      const request = net.request({
          method: 'GET',
          protocol: 'https:',
          url: 'http s://mydomain.com/somefile.php?data=something&moredata=somethingelse'
       });
    
      request.on("error", (err) => {
        console.log("NetError::: " +err.message);
       });
      
         request.on('response', (_response) => {
        _response.on('end', () => {
          console.log('NetEnd::: END response (no more data).');
        });
        _response.on('data', (_chunk) => {
            console.log("NetStatus::: " + _response.statusCode);
            console.log("NetHeaders::: " + JSON.stringify(_response.headers) );
            console.log("NetBody::: " + _chunk);
        });
    
     });
    
     request.end();
    

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 2016-07-21
      相关资源
      最近更新 更多