对于Angular 7,请使用HTTP API进行POST和缺少命令错误标头

时间:2019-02-07 08:43:49

标签: angular cors orientdb

我使用Angular 7和Orientdb 3.0.14。 我想连接到本地主机上的数据库demodb。

这是我的命令代码:

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

但是我在控制台上遇到此错误:

  

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

     

地图(7)大小:(...) proto :   地图[[条目]]:

     

Array(7)0:{“ access-control-allow-origin” =>   Array(1)}键:“ access-control-allow-origin”值:   [“ http://localhost:4200”] 1:{“ access-control-allow-methods” =>

     

Array(1)}键:“ access-control-allow-methods”值:[“ POST,GET,   选项,删除”]

     

2:{“ access-control-max-age” => Array(1)}键:   “访问控制最大年龄”值:[“ 3600”]

     

3:{“ access-control-allow-credentials” => Array(1)}键:   “ access-control-allow-credentials”值:[“ true”]

     

4:{“ access-control-allow-headers” => Array(1)}键:   “ access-control-allow-headers”值:[“ Content-Type,授权”]

     

5:{“授权” => Array(1)}键:“授权”值:[“基本   YWRtaW46YWRtaW4 =“]

     

6:{“ content-type” => Array(1)}键:   “内容类型”值:[“ application / json”]长度:7lazyInit:   nulllazyUpdate:nullnormalizedNames:   Map(7){“ access-control-allow-origin” =>   “ Access-Control-Allow-Origin”,“访问控制-允许方法” =>   “访问控制允许方法”,“访问控制最大年龄” =>   “ Access-Control-Max-Age”,“访问控制允许凭证” =>   “访问控制允许凭证”,“访问控制允许标题” =>   “ Access-Control-Allow-Headers”,…}

     

proto :对象core.js:16819   Angular在开发模式下运行。调用enableProdMode()来   启用生产模式。本地主机/:1访问XMLHttpRequest   原产地的“ http://localhost:2480/command/demodb/sql/-/-1”   “ http://localhost:4200”已被CORS政策屏蔽:对   预检请求未通过访问控制检查:否   请求中存在“ Access-Control-Allow-Origin”标头   资源。

请参阅Access-Control_allow-Origin进入标头。.但未被OriendDB接受

你能帮我吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

在OrientDB中,默认情况下未启用CORS,而您尝试实现的方式也没有帮助。

将此条目添加到@ecached('geturl_{url}', 3600) def get_with_cache(url): response: requests.Response = requests.get(url) return response.text

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 " />

同样重要的是不要在冒号<parameter value="utf-8" name="network.http.charset"/> 后使用空格