即使遵循2种方法,也会出现"访问控制 - 允许 - 来源"错误

DMA*_*DMA 5 marklogic cors marklogic-8 marklogic-7

我正在尝试构建一个角度应用程序来访问MarkLogic数据库中的数据.我正在使用MarkLogic rest API来访问数据.当我尝试运行该应用程序时,出现以下错误.

XMLHttpRequest无法加载 http://192.168.192.75:9550/v1/keyvalue?element=fieldId&value=1005&format=json.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.

我已经在stackoverflow上阅读了很多与此问题相关的答案,但无法正常工作.这是我到现在为止所尝试的.

1) Setting the response header using xdmp in qconsole
xdmp:add-response-header("Access-Control-Allow-Origin", "*");
xdmp:add-response-header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
xdmp:add-response-header("Access-Control-Allow-Headers", "x-requested-with, X-Auth-Token, Content-Type");
2) Tried to add headers by using REST [Extention][1]. Here is the example.sjs file which I wrote.
a) function get(context, params) {
  var results = [];
  context.outputTypes = [];
  for (var pname in params) {
    if (params.hasOwnProperty(pname)) {
      results.push({name: pname, value: params[pname]});
      context.outputTypes.push('application/json');
    }
  }
  context.outputStatus = [201, 'Created My New Resource'];
    context.outputHeaders = 
    {'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET, OPTIONS, DELETE', 'Access-Control-Allow-Headers' : 'x-requested-with, X-Auth-Token, Content-Type'};
  return xdmp.arrayValues(results);
};
exports.GET = get;
b) curl --anyauth --user admin:admin -X PUT -i -H "Content-type: application/vnd.marklogic-javascript" --data-binary @./example.sjs http://192.168.192.75:9550/LATEST/config/resources/example
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,它似乎都不起作用.任何人都可以告诉我,如果我做错了什么?或者如果有其他方法可以使这个工作?提前致谢.

DMA*_*DMA 1

我发现的另一种方法是定义我自己的转换。这仅对 GET 请求方法有帮助,对其他方法没有帮助。这是我创建的。

function customCors(context, params, content)
{
    xdmp.addResponseHeader('Access-Control-Allow-Origin' , '*');
    xdmp.addResponseHeader('Access-Control-Allow-Methods' , 'GET, PUT, POST, HEAD, OPTIONS, DELETE' );
    xdmp.addResponseHeader('Access-Control-Allow-Headers' , 'X-Requested-With, X-Auth-Token, Content-Type, Accept');
    return content;
};

exports.transform = customCors;
Run Code Online (Sandbox Code Playgroud)

要使用它,我们必须使用一个名为“transform”的属性。像这样。

http://192.168.192.75:9550/v1/keyvalue?element=fieldId&value=1005&transform=customCors&format=json

更新:联系了 MarkLogic,他们说这不能通过 2 层方法来完成。我们必须有一个像java这样的中间层(3层)来添加起源/方法等。