-1

I am building a headless-drupal application using reactjs, My application is basically running on a localhost:3000 while making http requests to the remote drupal 7 website. I have enabled CORS and was able to make a hppt POST request to login, i have the session id, token saved to my browser's local storage but when i try making get requests to the same server using superagent but the response returned is empty

{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()

I also made a similar request to the same server but to a different end point and it returned this error:

Access to XMLHttpRequest at 'http://website.com/endppoint-json' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. client.js:890 GET http://website.com/endppoint-json net::ERR_FAILED

this is what my code looks like:

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);
      });

please what have i not done correctly, this is my first time of making such http request and it is really giving a tough time please help, not insult please.

LSIMMON
  • 27
  • 10

1 Answers1

-1

I tried this, although it was not sent to a drupal website, i think this is cool

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