【问题标题】:How to make http get request to a drupal 7 website from reactjs application如何从reactjs应用程序向drupal 7网站发出http get请求
【发布时间】:2020-06-10 06:14:53
【问题描述】:

我正在使用 reactjs 构建一个 headless-drupal 应用程序,我的应用程序基本上在 localhost:3000 上运行,同时向远程 drupal 7 网站发出 http 请求。我已启用 CORS 并能够发出 hppt POST 请求以登录,我将会话 ID、令牌保存到浏览器的本地存储中,但是当我尝试使用 superagent 向同一服务器发出 get 请求但返回的响应为空时

{nodes: Array(0)}nodes: Array(0)length: 0__proto__: Array(0)proto: Objectconstructor: ƒ Object()defineGetter: ƒ defineGetter()defineSetter: ƒ defineSetter()hasOwnProperty: ƒ hasOwnProperty()lookupGetter: ƒ lookupGetter ()lookupSetter: ƒ lookupSetter()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toString: ƒ toString()valueOf: ƒ valueOf() toLocaleString: ƒ toLocaleString()get proto: ƒ proto()set proto: ƒ proto()

我也向同一个服务器发出了类似的请求,但发送到不同的端点,它返回了这个错误:

从源“http://localhost:3000”访问“http://website.com/endppoint-json”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。 client.js:890 GET http://website.com/endppoint-json net::ERR_FAILED

这是我的代码的样子:

superagent
      .get('http://website.com/endpoint-json')
      .set('Authorization', `Bearer ${this.getAuthenticationToken()}`)
      .end((err, res) => {
        if(err) {this.setState({errorMessage: 'Cannot retrieve geofences from server'}); return;}
        //this.setState({geofences: res.body});
        console.log(res.body);
      });

请问我做错了什么,这是我第一次提出这样的http请求,真的很艰难,请帮助,不要侮辱。

【问题讨论】:

    标签: json reactjs api cors superagent


    【解决方案1】:

    我试过了,虽然它没有发送到drupal网站,但我觉得这很酷

    async componentDidMount() {
        const url = "https://website.com/fence-json_test";
        const response = await fetch(url);
        const data = await response.json();
        console.log(data);
      }
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 1970-01-01
      • 2017-03-09
      • 2012-05-13
      • 2018-02-07
      • 2018-11-16
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      相关资源
      最近更新 更多