【问题标题】:MarkLogic 7 REST API - Bad Request ErrorMarkLogic 7 REST API - 错误请求错误
【发布时间】:2014-05-04 12:45:19
【问题描述】:

我正在使用 AngularJS 在 MarkLogic 7 XML 数据库之上构建一个 web 应用程序。我正在使用 MarkLogic 提供的 REST API 来访问数据库。我在控制器中使用的代码如下。

app.controller("mainController", function($scope, $http){
    var addr = 'http://localhost:8011/v1/search?q=test&options=newtest';
    $http({method:'GET', url: addr})
    .success(function(data){
    alert('Success');
        console.log(data);
});
});

现在,当我打开视图时,控制台中会记录一个“错误请求”错误。

OPTIONS http://localhost:8011/v1/search?q=test&options=newtest 400 (Bad Request)
angular.min.js:99
OPTIONS http://localhost:8011/v1/search?q=test&options=newtest No 'Access-Control-Allow-  
Origin' header is present on the requested resource. Origin 'null' is therefore not 
allowed access. angular.min.js:99
XMLHttpRequest cannot load http://localhost:8011/v1/search?q=test&options=newtest. No  
'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null'   
is therefore not allowed access. index.html:1

是否需要在 MarkLogic 服务器上配置任何设置以使 API 可访问?请帮忙。

谢谢, 克里希纳

【问题讨论】:

  • 搜索错误信息,No 'Access-Control-Origin' header is present on the requested resource
  • 您好 Jason,我已尝试按照针对该问题的建议使用 jsonp。但是,我不能在请求中使用回调参数,因为 MarkLogic 不理解它。此外,当我尝试使用 $http.jsonp(url).success(data) 方法时,它给了我一个意外的 ':' 错误。我试图通过在端口号前转义“:”来解决此问题,但随后出现“连接被拒绝”错误。所以那个解决方案对我不起作用。

标签: json angularjs rest marklogic


【解决方案1】:

Krishna,你的 AngularJS 代码是由 MarkLogic 托管的,还是在 node.js 中运行(也许是通过 grunt)?这看起来像一个跨站点脚本问题。想到了两个解决方案:

  1. 如果您不需要中间层,MarkLogic 可以托管 Angular 代码。将它放在端口 8011 上的应用程序服务器的模块数据库中。然后请求将转到 JS 来自的同一个地方,问题就消失了。
  2. 如果要保留节点层,请设置代理。假设您使用的是 Grunt,您可以在具有相同堆栈的应用程序的 Gruntfile.js 中看到我是如何处理它的。

克服该错误后,您可能会考虑将与 MarkLogic REST API 的交互转移到服务中。我上面链接的演示猫应用程序有an early version of an MLRest AngularJS service;这可能是一个有用的起点。

【讨论】:

  • 戴夫,谢谢您的回复。我将 HTML 页面托管在运行在端口 8080 上的 Tomcat 服务器上。我现在已将所有文件加载到模块数据库中,但我现在如何访问 HTML 页面?
  • HTML 可以托管在模块 DB 中的 JS 文件旁边……MarkLogic 可以很好地提供静态内容。
  • 例如,假设您在端口 8010 上有一个 MarkLogic HTTP 应用程序服务器,并且您的模块数据库中的 my-page.html 位于 /my-page.html(位于根目录)。然后您可以将浏览器指向localhost:8010/my-page.html。另请注意,您可以设置重写器将 URL 映射到 html 页面或 XQuery 主模块。
猜你喜欢
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多