Martel
Martel

Reputation: 33

With Angular 7 use HTTP API for POST and command error headers missing

I use Angular 7 and Orientdb 3.0.14. I want connect to my database demodb in localhost.

this my code for command :

command(statement: string, success: (data: any) => void, error: (err: any) => void): void {
    this.url = this.url + 'sql/-/-1';
    this.headers = new HttpHeaders()
      .set('Access-Control-Allow-Origin', 'http://localhost:4200')
      .set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE')
      .set('Access-Control-Max-Age', '3600')
      .set('Access-Control-Allow-Credentials', 'true')
      .set('Access-Control-Allow-Headers', 'Content-Type, authorization')
      .set('Authorization', 'Basic ' + btoa(this.username + ':' + this.password))
      .set('Content-Type', 'application/json');
      console.log(this.headers);
    this.http.post(this.url,
      JSON.stringify({'command': statement}),
      {headers: this.headers})
      .toPromise()
      .then(success)
      .catch(error);

but i have this error on console:

HttpHeaders {normalizedNames: Map(0), lazyUpdate: Array(7), headers: Map(0), lazyInit: HttpHeaders}headers:

Map(7)size: (...)proto: Map[[Entries]]:

Array(7)0: {"access-control-allow-origin" => Array(1)}key: "access-control-allow-origin"value: ["http://localhost:4200"]1: {"access-control-allow-methods" =>

Array(1)}key: "access-control-allow-methods"value: ["POST, GET, OPTIONS, DELETE"]

2: {"access-control-max-age" => Array(1)}key: "access-control-max-age"value: ["3600"]

3:{"access-control-allow-credentials" => Array(1)}key: "access-control-allow-credentials"value: ["true"]

4:{"access-control-allow-headers" => Array(1)}key: "access-control-allow-headers"value: ["Content-Type, authorization"]

5:{"authorization" => Array(1)}key: "authorization"value: ["Basic YWRtaW46YWRtaW4="]

6: {"content-type" => Array(1)}key: "content-type"value: ["application/json"]length: 7lazyInit: nulllazyUpdate: nullnormalizedNames: Map(7) {"access-control-allow-origin" => "Access-Control-Allow-Origin", "access-control-allow-methods" => "Access-Control-Allow-Methods", "access-control-max-age" => "Access-Control-Max-Age", "access-control-allow-credentials" => "Access-Control-Allow-Credentials", "access-control-allow-headers" => "Access-Control-Allow-Headers", …}

proto: Object core.js:16819 Angular is running in the development mode. Call enableProdMode() to enable the production mode. localhost/:1 Access to XMLHttpRequest at 'http://localhost:2480/command/demodb/sql/-/-1' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

see Access-Control_allow-Origin is into header..but not accepted by OriendDB

Can you help me ? thanks.

Upvotes: 2

Views: 908

Answers (1)

TheParam
TheParam

Reputation: 10541

In OrientDB CORS is not enabled by default and the way you trying to achieve not helpful.

Add this entry in config/orientdb-server-config.xml

<parameter name="network.http.additionalResponseHeaders" value="Access-Control-Allow-Origin: * ;Access-Control-Allow-Credentials: true;Access-Control-Allow-Headers: Content-Type;Access-Control-Allow-Methods: POST, GET, DELETE, HEAD, OPTIONS, PATCH, CONNECT, TRACE " />

above

<parameter value="utf-8" name="network.http.charset"/>

It's also important to not use space after the colon ;

Upvotes: 1

Related Questions