我在Express中的req.body有什么问题,它不会让我解析JSON?
我正在从我的前端向我正在从Nodejs提供的Express路由处理程序发出一个ajax POST请求。
我的数据是按照以下方式从结尾发送的,如输出到控制台所确认的:
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
在后端,我得到了适当的请求对象。当我尝试使用以下语句将其记录到控制台时:
console.log("This is the request from the clientSide..." + JSON.stringify(req.body));
我在终端上得到这个:
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\"}":""}
为什么请求在引号之前添加\
be?
我怀疑这是我问题的根源。我无法解析请求对象。我尝试过使用:
req.body["panReferenceID"]
例如,要获取请求对象中的第一个数据项,我在终端中得到“未定义”...
ajax post请求发生了什么,为什么我无法解析它?
有人看到我在这里看不到的错误???
谢谢..
编辑
这是我的客户端代码。当我将它输出到控制台然后再支持到后端它很好,在后端它变为看起来像它有空值:
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);
}
});
}
回答如下:
var bodyParser = require('body-parser')
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())