AngularJS connecting to MarkLogic

I am facing below issue while connecting to MarkLogic from AngularJS

XMLHttpRequest cannot load http://127.0.0.1:8000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost.:7080' is therefore not allowed access. The response had HTTP status code 401.

Code Used to connect is as below

var data1=$scope.user;
alert(data1.eMail);
$http({
    type: 'POST',
    url: 'http://127.0.0.1:8000/',
    data: data1,
    format: 'json'
}).success(function () {
     alert('Successfully Added In DB');
});

here data1 is my json object, which contains user entered information

url1 is my marklogic server path and i am trying to connect from my App http://localhost:7080/

I have even gone through couple of web sites like https://github.com/marklogic/slush-marklogic-node, but due to code complexity am unable to understand clearly. can anyone help me in giving simple steps to connect to MarkLogic from AngularJS or correct me in modifying my code written above.

I have added

xdmp.addResponseHeader("Access-Control-Allow-Origin", "*"); also in My MarkLogic.

Upvotes: 0

Views: 425

Answers (2)

Shon
Shon

Reputation: 700

There are a few options to solve your CORS problem with MarkLogic's REST API:

Create a thin middle tier. Slush is a solid reference for the 3-tier architecture which uses Node.js as a thin middle layer. Samplestack is another example of a 3-tier solution.

Host your client assets in the same modules database as your REST API server. With this 2-tier approach, your HTTP app server is using the same port assignment, so no cross-origin issues. See @davecassel's answer here for more info.

At present you can't add response headers to the built-in endpoints, you would have to create custom resource extensions.

To use built-ins, you could possibly use a transparent reverse proxy to reroute requests with Apache. See @mblakele's answer here on this.

For more details you could search SO using the tag as there are other CORS-related Q&A.

Upvotes: 0

Raphaël Braud
Raphaël Braud

Reputation: 1519

It seems you're facing cross origin issues. This should be solved in the backend part by adding cors header to allow (or not) requests from clients.

For instance, this header : Access-Control-Allow-Origin: * would allow any requests. Normally you just want to allow known clients/ips

This has to be done in MarkLogic (which I don't know)...

Upvotes: 1

Related Questions