我正在 MarkLogic 7 XML 数据库之上使用 AngularJS 构建一个 Web 应用程序。我正在使用 MarkLogic 提供的 REST API 来访问数据库。我在 Controller 中使用的代码如下。
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);
});
});
现在,当我打开 View 时,控制台中会记录“错误请求”错误。
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?请帮忙。
谢谢, 克里希纳
最佳答案
Krishna,您的 AngularJS 代码是由 MarkLogic 托管的,还是在 Node.js 中运行(也许通过 grunt)?这看起来像是跨站点脚本问题。我想到了两种解决方案:
- 如果您不需要中间层,MarkLogic 可以托管 Angular 代码。将其放入端口 8011 上的应用程序服务器的模块数据库中。然后请求将发送到 JS 的同一位置,问题就消失了。
- 如果您想保留节点层,请设置代理。假设您正在使用 Grunt,您可以在 Gruntfile.js 中看到我是如何处理它的。具有相同堆栈的应用程序。
一旦克服了该错误,您可能会考虑将与 MarkLogic REST API 的交互移至服务中。我上面链接的演示猫应用程序有 an early version of an MLRest AngularJS service ;这可能是一个有用的起点。
关于json - MarkLogic 7 REST API - 错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22655512/