0

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);
                }
            });

        }
lopezdp
  • 1,538
  • 3
  • 21
  • 38
  • You're stringifying a string. You need to `JSON.parse(req.body).panReferenceID` – Patrick Roberts Dec 17 '17 at 06:21
  • that throws an error: SyntaxError: Unexpected token o in JSON at position 1 – lopezdp Dec 17 '17 at 06:22
  • 2
    Possible duplicate of [I can't get values from req.body node.js](https://stackoverflow.com/questions/32810728/i-cant-get-values-from-req-body-node-js) Looking more carefully at the output of your `JSON.stringify()`, your object is actually stored as a key in the stringified JSON, and the value for the key is an empty string. This looks like a client-side encoding error. – Patrick Roberts Dec 17 '17 at 06:30
  • To clarify, on the client-side, it appears you're doing something like this: `var object = {}; object[JSON.stringify(data)] = ""; var body = JSON.stringify(object);` and sending that. – Patrick Roberts Dec 17 '17 at 06:34
  • you can use body parser library – zabusa Dec 17 '17 at 06:37
  • @PatrickRoberts check out the edit. on the client side it is formatted correctly in console, in back end it shows up with that empty value. – lopezdp Dec 17 '17 at 06:43
  • @zabusa I am using bodyParser and everything is included that is required. – lopezdp Dec 17 '17 at 06:43
  • Can you share the bodyParser code you put in the backend ? – Malice Dec 17 '17 at 06:59
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – lopezdp May 29 '18 at 02:28

1 Answers1

0
var bodyParser = require('body-parser')

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())
zabusa
  • 2,520
  • 21
  • 25