Javascript Code Samples
When utilizing Javascript to interact with MWS, it is recommended to use libraries
that provide a simple browser-independent syntax for performing REST calls. It must
also be noted that it is not recommended to make calls to MWS directly from client-
side Javascript, as this will contain the username and password for MWS and
could potentially be retrieved and utilized by a malicious user.
Examples for specific libraries are given below.
jQuery
The jQuery
ajax
function makes it extremely simple to perform all REST calls.
There are also some shorthand methods which utilize the
ajax
method
in a simple manner, such as
getJSON
and
post
, but these do not have the ability
to specify authentication parameters, which is why they are not used below.
In all examples given, it is assumed that the
$
variable is mapped to
jQuery
, which
is the default configuration of jQuery.
GET
$.ajax({
url: "http://localhost:8080/mws/rest/jobs",
dataType: 'json',
username: 'admin',
password: 'secret',
success: new function(data) {
$('.result').html(data);
alert("GET was successful");
)
});
POST
$.ajax({
url: "http://localhost:8080/mws/rest/jobs",
data: {
"commandFile":"/tmp/test.sh",
"initialWorkingDirectory":"/tmp",
"user":"adaptive",
"requirements":[{"requiredNodeCountMinimum":1}]
}
dataType: 'json',
username: 'admin',
password: 'secret',
success: new function(data) {
$('.result').html(data);
alert("GET was successful");
)
});
PUT
$.ajax({
type: 'PUT',
url: 'http://localhost:8080/mws/rest/jobs/Moab.1',
data: {"holds":["user"]},
dataType: "json",
username: 'admin',
password: 'secret',
success: function(data) {
$('.result').html(data);
alert("PUT was successful");
}
});
DELETE
$.ajax({
type: 'DELETE',
url: 'http://localhost:8080/mws/rest/jobs/Moab.1',
username: 'admin',
password: 'secret',
success: function(data) {
$('.result').html(data);
alert("DELETE was successful");
}
});