【问题标题】:Request cannot be constructed from a URL that includes credentials无法从包含凭据的 URL 构造请求
【发布时间】:2017-07-20 00:01:00
【问题描述】:

我想从 React.js 中的 API 获取 JSON。我尝试过使用 axios、superagent 和 fetch,但它不起作用?

let token = '****';
    let url = 'https://'+ token +'@api.navitia.io/v1/coverage/fr-idf/stop_areas/stop_area%3AOIF%3ASA%3A59491/departures?';

    let myInit = {
        'method': 'GET'
    }

    fetch(url, myInit).then((response)=>{
        return response.json();
    }).then((data)=> {
        console.log('ok');
    }).catch(function(err){
        console.log('Erreur: ' + err);
    });

错误:“无法从包含凭据的 URL 构造请求”

【问题讨论】:

    标签: api reactjs fetch-api


    【解决方案1】:

    我认为错误是让您知道问题是它不以这种方式接受凭据。

    您需要创建一个 X-Auth-Token 标头并将令牌添加到该标头。然后通过整个事情

    【讨论】:

      【解决方案2】:

      试试这个改变

      let myInit = {
          'method': 'GET',
          credentials: 'include' // or 'same-origin'
      }
      

      【讨论】:

        猜你喜欢
        • 2017-12-17
        • 2022-01-01
        • 1970-01-01
        • 2019-12-09
        • 1970-01-01
        • 2019-12-04
        • 1970-01-01
        • 2017-12-15
        • 1970-01-01
        相关资源
        最近更新 更多