on 02-11-2015 6:14 PM
Hi,
I am making an Ajax GET request and sending a json object. I am able to capture the string values in xsjs whereas for the array, it says undefined. How can I get the Array values? Here is how the code looks like:
Ajax Call:
in the data: results is an array.
$.ajax({
url : getServiceUrl("createUserTags.xsjs"),
type : "GET",
data : {"objectId" : objectId , "userId" : oUserId , "tagsArray" : results},
async : false,
dataType : "json",
success : function(data){
console.log(data);
}
});
in my xsjs file:
var objectId = $.request.parameters.get("objectId");
var tagsArray = $.request.parameters.get("tagsArray"); //---------> I think this is where the issue is
var userId = $.request.parameters.get("userId");
var conn = $.db.getConnection();
try{
for (var c = 0; c< tagsArray.length; c++){
// do something....
}
conn.commit();
$.response.setBody('{"success": "true" }');
$.response.headers.set("Access-Control-Allow-Origin", "*");
$.response.contentType = "application/json";
}catch(e){
$.response.setBody('{"Fail": "' + tagsArray + '"}');
$.response.headers.set("Access-Control-Allow-Origin", "*");
$.response.contentType = "application/json";
}
I get the error message as {"Fail" : "undefined"}, how can I get the tagsArray values?
Thank you
Prab
Hi Prab,
you can get the array parameter by adding brackets to the parameter name in the get call.
var tagsArray = $.request.parameters.get("tagsArray[]");
This is necessary, because in the request URL the parameter "tagsArray" is called "tagsArray%5b%5d" (= "tagsArray[]" ) because of the array value.
Best regards,
Florian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
var data1=
{
ID:oInput.getValue() ,
FirstName:oInput1.getValue(),
LastName:oInput2.getValue(),
DoB:oInput3.getValue(),
Phone:oInput4.getValue(),
};
var datavalue=JSON.stringify(data1);
$.ajax({
url: jurl,
type: 'POST',
// contentType: 'application/json',
data:datavalue,
/* data:{
"dataobject":datavalue
},*/
// processData: false,
dataType: 'json',
success: function(sentimentData, textStatus, jqXHR) { // callback called when data is received
// sap.ui.commons.MessageBox.show(jqXHR.responseText, "Success", "Service call error")
alert(sentimentData);
},
error: function(jqXHR, textStatus, errorThrown){
alert("error");
// sap.ui.commons.MessageBox.show(jqXHR.responseText, "ERROR", "Service call error");
}
});
xsjs
$.response.contentType = "application/text";
var body='';
//var aCmd = $.request.parameters.get('dataobject');
//var obj=JSON.stringify(aCmd);
//var obj= JSON.parse(aCmd);
//$.response.setBody(JSON.stringify(aCmd));
/*var sJSONBody = $.request.body.asString();
var overallsoData = JSON.parse(body);*/
var id =$.request.parameters.get("ID");
var firstname = $.request.parameters.get("FirstName");
var lastname = $.request.parameters.get("LastName");
var dob = $.request.parameters.get("DoB");
var phone = $.request.parameters.get("Phone");
var tx_data_query = "";
function getTxtData()
{
//var txlist = [];
var connection = $.db.getConnection();
var statement = null;
var resultSet = null;
var id= obj.ID.toString() ;
var firstname=obj.FirstName.toString();
var lastname=obj.LastName.toString();
var dob=obj.DoB.toString();
var phone=obj.Phone.toString();//11var url="kapil";
tx_data_query = 'INSERT INTO table_name (ID, FIRST_NAME, LAST_NAME, DOB, PHONE_NUMBER) VALUES (' + $.request.parameters.get('ID') + ',\'' + firstname + '\',\'' + lastname + '\',\'' + dob + '\',\'' + phone + '\')';
// tx_data_query = 'INSERT INTO \"_150949\".\"EMPLOYEE_DETAILS\" (ID, FIRST_NAME, LAST_NAME, DOB, PHONE_NUMBER) VALUES (51,\'kapil\',\'jain\',\'2014-12-1\',\'95656445\')';
try
{
statement = connection.prepareStatement(tx_data_query);
resultSet=statement.executeQuery();
connection.commit();
var txtItem;
while(resultSet.next())
{
txtItem = {};
txtItem.Region = resultSet.getString(1);
txlist.push(txtItem);
}
} finally {
// resultSet.close();
statement.close();
connection.close();
}
return resultSet;
}
/*getTxtData();*/
function doGet()
{
try
{
$.response.contentType = "application/json";
$.response.contentType = "text/plain";
$.response.setBody(getTxtData());
}
catch(err)
{
$.response.contentType = "text/plain";
$.response.setBody("Error while executing query: [" +err.message +"]");
$.response.returnCode = 200;
}
}
doGet();
Hi Kapil,
From quick look I see you are making a POST request where as in xsjs, the way you are trying to get the required variables by doing "var id =$.request.parameters.get("ID");" works for a GET request.
For a POST request try something like as below in xsjs:
var obj = $.request.body.asString();
obj = JSON.parse(obj);
var itemId = obj.ID;
var firstName = obj.FirstName;
Hope this helps.
Hi,
for those who are still refer to this question, I find very useful the XS Classic Javascript library:
https://help.sap.com/doc/3de842783af24336b6305a3c0223a369/2.0.03/en-US/index.html
Best regards,
Silviu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.