Semaphor blog
Blog: Semaphor

Using JSONP for exchanging data across domains

If you are building a website and would like to make a jQuery call to a LotusScript agent in an IBM Notes database on another domain, you are likely to run into security issues in your browser.

When you POST data to an agent via jQuery, the agent will run, but the response will not be available in the browser.

One way to solve the problem is to use JSONP (JSON with Padding) which let you define a callback function to return your data

myCallback(
{
  "status": "success",
		"code": 4,
		"message": "This is a message from another subdomain."
	}
);


If you place the callback function in a LotusScript agent, you will need to set the content-type to application/json.

Sub Initialize()
	Print "Content-Type: application/json" + Chr(13)
	Print |myCallback(
	{
		"status": "success",
		"code": 4,
		"message": "This is a message from another subdomain."
	}
	);| + Chr(13)
End Sub


Next step is to request the data:

function myDemo() {
//var url = 'http://www.semaphor.dk/database.nsf/jsonp_demo?openagent'; THIS WONT WORK!!!
var url = 'http://www.semaphor.dk/jsonp_demo/';
	$.ajax({
	   type: 'GET',
	    url: url,
	    async: false,
	    jsonpCallback: 'myCallback',
	    contentType: "application/json",
	    dataType: 'jsonp',
	    success: function(data) {
	       alert(data.message);
	    }
	});
}


The JSONP callback will add "?callback=jsonp" to the url so you can not include "?openagent" in the URL.
Instead, I use a server redirection that points /jsonp_demo/* to /database.nsf/jsonp_demo?openagent&parameters=*


If you wish to pass a parameter to the agent, you can add it after the / and modify your agent to react on them as needed.

function myDemo() {
var url = 'http://www.semaphor.dk/jsonp_demo/';
url = url + '&firstname=' + $("#firstname").val() + '&lastname=' + $("#lastname").val();
	$.ajax({
	   type: 'GET',
	    url: url,
	    async: false,
	    jsonpCallback: 'myCallback',
	    contentType: "application/json",
	    dataType: 'jsonp',
	    success: function(data) {
	       alert(data.message);
	    }
	});
}

Firstname:
Lastname:


I hope this simple example will get you started.
-Rune
25-09-2013

0 Comments

Add comment

Name:
E-mail:
City:
Job:
Subject:
Comment:
 
It may take a moment until your comment is published.