Hugh A. Tucker
Hugh A. Tucker

Reputation: 31

using jQuery AJAX to interact with markLogic

I would like to use markLogic as a document store -- and I would be very pleased if someone could list a simple AJAX method to access mL. I have loaded mL on my PC - my localhost is pointing at Apache (WAMPserver). I am uploading data (json & xml) from a 3rd party site (yahoo.com) and after massaging it I would like to store it in mL using a simple jquery AJAX function. Please no 3rd party s/w such as ROXY, etc.

Upvotes: 0

Views: 390

Answers (2)

mg_kedzie
mg_kedzie

Reputation: 437

Make sure that you have REST service that gets data from Marklogic database:

curl --basic --user admin:none -i -X POST -H "Content-type: application/x-www-form-urlencoded" --data-urlencode module=/ext/invoke/test.xqy --data-urlencode vars='{"j":{"name":"John","bankAccounts":["Northern Bank","Fargo"]}}' http://ec2-18-217-208-58.us-east-2.compute.amazonaws.com:8003/LATEST/invoke

Below is my index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<title>Marklogic Ajax Test</title>
<meta name="description" content="Marklogic AJAX Test Client" />
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript" >
$(function() {
		$('#submit').button();
		$("#form").submit(function(e) {
			var user = $('#user').val();
			var password = $('#password').val();
			var url = $('#url').val();
			e.preventDefault();
			$.ajax({
				   type: "POST",
				   url: url,
				   data: $("#form").serialize(),
				   beforeSend: function (xhr) {
						xhr.setRequestHeader ("Authorization", "Basic " + btoa(user + ":" + password));
					},
				   success: function(data)
				   {
					   alert(data); // show response from the php script.
				   },
				   error: function(data)
				   {
					   alert("Error: " + data);
				   }
				 });
		});
  });
</script>
</head>
<body>
Enter URL of REST service:  <input type="text" name="url" value="http://ec2-13-58-46-47.us-east-2.compute.amazonaws.com/marklogic/LATEST/invoke " id="url" style='width:70em' /> <br/>
Username: <input type="text" name="username" value="admin" id="user"/> <br/>
Password: <input type="password" name="password" value="none" id="password"/> <br/>
<h1>Invoke REST service</h1>
<form id="form" method="post" enctype="application/x-www-form-urlencoded">
 URI of xquery in module database:  <input type="text" name="module" value="/ext/invoke/test.xqy"style='width:70em' ><br>	
 JSON input: <input type="text" name="vars" value='{"j":{"name":"John","bankAccounts":["Northern Bank","Fargo"]}}' style='width:70em'/> <br/>
<input id="submit" type="submit" value="invoke xquery">
</form>
</body>
</html>

Add to your /etc/httpd/conf/httpd.conf:

ProxyPass /marklogic/ http://localhost:8003/ <Location /marklogic/> ProxyPassReverse / #ProxyHTMLEnable On SetOutputFilter INFLATE;proxy-html;DEFLATE #ProxyHTMLURLMap http://localhost:8003/ /marklogic/ #ProxyHTMLURLMap / /marklogic/ </Location>

The code was tested, but I did not give you valid password. It is possible that you will have to modify ProxyPass section.

Upvotes: 0

wst
wst

Reputation: 11771

You will first need to bootstrap a REST server for your database. Start here:

http://developer.marklogic.com/learn/rest/setup#create-a-rest-api-instance

Then you can skip to the section on CRUD.

Upvotes: 5

Related Questions