I am making an ajax POST request from my front end to an Express route handler I am serving from Nodejs.
My data is being sent from the from end in the following manner as confirmed by output to console:
This data is sent from front.end: {"panReferenceID":"0987","walletAccountEmailAddressHash":"8907","clientWalletAccountId":"879","visaTokenScore":"0987","visaTokenDecisioning":"879","addressVerificationResultCode":"rty67u89i9o0","cvv2ResultsCode":"897","locale":"34567890","deviceInfo":{"deviceId":"hgjk","deviceLanguageCode":"890","osVersion":"ertyuio","osBuildID":"879","deviceIDType":"87","deviceManufacturer":"9876","deviceBrand":"897","deviceModel":"34567890","deviceName":"980","deviceNumber":"980","deviceLocation":"9876","deviceIpAddressV4":"789"},"encryptedData":"87"},http://localhost:3000/vtis/tokenRequestors/5/tokens/987/tokenChanged?eventType=TOKEN_CREATED&eventID=254
On the backend, I get the appropriate request object. When I try to log it to the console with the following statement:
console.log("This is the request from the clientSide..." + JSON.stringify(req.body));
I get this in my terminal:
This is the request from the clientSide...{"{\"panReferenceID\":\"0987\",\"walle
tAccountEmailAddressHash\":\"8907\",\"clientWalletAccountId\":\"879\",\"visaToke
nScore\":\"0987\",\"visaTokenDecisioning\":\"879\",\"addressVerificationResultCo
de\":\"rty67u89i9o0\",\"cvv2ResultsCode\":\"897\",\"locale\":\"34567890\",\"devi
ceInfo\":{\"deviceId\":\"hgjk\",\"deviceLanguageCode\":\"890\",\"osVersion\":\"e
rtyuio\",\"osBuildID\":\"879\",\"deviceIDType\":\"87\",\"deviceManufacturer\":\"
9876\",\"deviceBrand\":\"897\",\"deviceModel\":\"34567890\",\"deviceName\":\"980
\",\"deviceNumber\":\"980\",\"deviceLocation\":\"9876\",\"deviceIpAddressV4\":\"
789\"},\"encryptedData\":\"87\"}":""}
Why is the request adding a \
before the quotes???
I suspect this is the root of my problem. I cant parse the request object. I have tried using:
req.body["panReferenceID"]
For example, to get the first data item in the request object, however, I get "undefined" in my terminal...
What is going on with the ajax post request and why am I not able to parse it?
Anybody see the error I am not seeing here???
Thanks..
EDIT
This is my client side code. When I output it to console before siding to back end it is fine, in the back end it changes to look like it has that empty value:
var deviceInfo = {
'deviceId':deviceID, 'deviceLanguageCode': deviceLanguageCode,
'osType':osType, 'osVersion':osVersion, 'osBuildID':osBuildID,
'deviceType': deviceType, 'deviceIDType':deviceIDType,
'deviceManufacturer':deviceManufacturer, 'deviceBrand':deviceBrand,
'deviceModel':deviceModel, 'deviceName':deviceName, 'deviceNumber':deviceNumber,
'deviceLocation':deviceLocation, 'deviceIpAddressV4':deviceIpAddressV4,
'locationSource':locationSource,'tokenProtectionMethod':tokenProtectionMethod
};
// Need a JWE encryption mechanism for JWE data
var requestPayload = {
'panReferenceID':panReferenceID,
'walletAccountEmailAddressHash':walletAccountEmailAddressHash,
'clientWalletAccountId':clientWalletAccountId,
'visaTokenScore':visaTokenScore,
'visaTokenDecisioning':visaTokenDecisioning,
'panSource':panSource,
'addressVerificationResultCode':addressVerificationResultCode,
'cvv2ResultsCode':cvv2ResultsCode,
'consumerEntryMode':consumerEntryMode,
'locale':locale,
'deviceInfo':deviceInfo,
'encryptedData':encryptedData
}
// reset form on submit
// document.form["#tcnform"].reset()
// async call to post form data to server to wait for response
$.ajax({
url: 'http://localhost:' + clientPort.toString() + '/vtis/tokenRequestors/' + inputTokenRequestorID + '/tokens/' + inputTokenReferenceID + '/tokenChanged?eventType=' + eventType + '&eventID=' + eventID,
data: requestPayload,
dataType: 'json',
type: 'POST',
Content-Type: 'application/json',
success: function(data, textStatus){
console.log("DEBUG: Response from server: " + data);
console.log("Sent Token Create Notification: " + data);
console.log("This data is sent from front.end: " + this.data + "," + this.url);
// Need to get data from server based on tests and load to text field for copy by user
},
error: function(request, status, error){
var val = request.responseText;
console.log("Error in Ajax: " + val);
}
});
}