With JavaScript, you can pass function arguments by using the 'args' parameters.
However, it might be a tricky thing because you have to keep in mind these quirks:
Do not use single quotes to wrap 'args' elements because JSON uses double quotes. |
|
Escape double quotes in 'args' elements, if any. |
This page includes some simple examples how you can pass function arguments by using the 'args' parameters easily and properly.
#1 – Basic
This example shows how to specify the 'args' parameters properly in a general case.
$.ajax({
type: "POST",
data: 'args=["parameter #1","parameter #2","parameter #3"]',
url: "http://localhost:9090/LyciaWeb/esapi/call/my_web_api_server",
xhrFields: {
withCredentials: true
},
success: function (response) {
console.log(response);
}
});
#2
This example shows how to concatenate 'args' values.
var parameter1 = "parameter #1";
var parameter2 = "parameter #2";
var parameter3 = "parameter #3";
$.ajax({
type: "POST",
data: 'args=["' + parameter1+'","'+parameter2+'","'+parameter3+'"]',
url: "http://localhost:9090/LyciaWeb/esapi/call/my_web_api_server",
xhrFields: {
withCredentials: true
},
success: function (response) {
console.log(response);
}
});
#3
This example shows how to pass JavaScript objects as 'args' parameters (includes using JSON.stringify, escaping double quotes, and concatenating 'args' values).
var object1 = {
name1: "object1-value1",
name2: "object1-value2",
name3: "object1-value3"
};
var object2 = {
name1: "object2-value1",
name2: "object2-value2",
name3: "object2-value3"
};
var object3 = {
name1: "object3-value1",
name2: "object3-value2",
name3: "object3-value3"
};
var parameter1 = JSON.stringify(object1);
var parameter2 = JSON.stringify(object2);
var parameter3 = JSON.stringify(object3);
var data = {
args: '["' +
parameter1.replace(/\"/g, '\\\"')+'","'+
parameter2.replace(/\"/g, '\\\"')+'","'+
parameter3.replace(/\"/g, '\\\"')+
'"]'
};
$.ajax({
type: "POST",
data: data,
url: "http://localhost:9090/LyciaWeb/esapi/call/my_web_api_server",
xhrFields: {
withCredentials: true
},
success: function (response) {
console.log(response);
}
});
Related articles: