json - MarkLogic 7 REST API - 错误请求错误

标签 json angularjs rest marklogic

我正在 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)?这看起来像是跨站点脚本问题。我想到了两种解决方案:

  1. 如果您不需要中间层,MarkLogic 可以托管 Angular 代码。将其放入端口 8011 上的应用程序服务器的模块数据库中。然后请求将发送到 JS 的同一位置,问题就消失了。
  2. 如果您想保留节点层,请设置代理。假设您正在使用 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/

相关文章:

java - 流式写Json

php - 如何像数组对象一样访问 PHP 中的 JSON 元素?

angularjs - 如何为 ng-grid angularjs 创建自定义排序函数

javascript - AngularJS 自定义指令和 ng Repeat

java - 在 Camel 上使用 Springboot 的 Rest DSL 在重新启动 EAP 后创建路由抛出异常

asp.net-mvc - 如何在ASP MVC中返回JSON结构

javascript - 加载 JSON 并将其显示在我的 Angular 应用程序中的所有 View 上

angularjs - Protractor - 通过子元素过滤时获取父元素的兄弟元素

javascript - 将 javascript 变量设置为函数的返回值

java - Nodejs 在单线程环境中进行许多剩余调用