我正在编写一个在NODE.JS中获取JSON的api。发送数据的方法是POST。
为简化请求的管理,我使用lib Express和BodyParser解释POST请求的正文。
我正在将信息归类为javascript对象:
const data = {
"plan": "312409959F9FDDE444959F9C950201D7",
"sender": {
"name": $("[name=pname]").val(),
"email": $("[name=pemail]").val(),
"hash": credentials.hash,
"phone": {
"areaCode": $("[name=pphone]").val().substring(0, 2),
"number": $("[name=pphone]").val().substring(2, 11)
},
"address": {
"street": $("[name=paddress]").val(),
"number": $("[name=pnumber]").val(),
"complement": $("[name=pcomplement]").val(),
"district": $("[name=pbairro]").val(),
"city": $("[name=pcity]").val(),
"state": $("[name=puf]").val(),
"country": "BRA",
"postalCode": $("[name=pcep]").val()
},
"documents": [{
"type": "CPF",
"value": $("[name=pcpf]").val()
}]
},
"paymentMethod": {
"type": "CREDITCARD",
"creditCard": {
"token": credentials.token,
"holder": {
"name": $("[name=pownername]").val(),
"birthDate": $("[name=pbirthday]").val().split('-').reverse().join('/'),
"documents": [{
"type": "CPF",
"value": $("[name=pcpf2]").val()
}],
"phone": {
"areaCode": $("[name=pphone2]").val().substring(0, 2),
"number": $("[name=pphone2]").val().substring(2, 11)
}
},
}
}
}
请求发送如下:
$.post('/adherence', data, function (msg) {
console.log(msg)
})
我后端的收据是这样的:
app.post('/adherence', (req, res) => {
try {
console.log(req.body);
res.send("OK");
} catch (e) {
console.log(e);
res.send(e);
}
});
但是我在req.body中得到的是:
{ plan: '312409959F9FDDE444959F9C950201D7',
'sender[name]': 'FRancisco',
'sender[email]': 'falisson.sv@sandbox.pagseguro.com.br',
'sender[hash]': 'ceedf0fd2ffd35f4054104d305088e19e8ca9333bbf70be10bd2ea6f94af226a',
'sender[phone][areaCode]': '63',
'sender[phone][number]': '991047876',
'sender[address][street]': '1105 sul qi 3 al 3 lt 1',
'sender[address][number]': '1',
'sender[address][complement]': '',
'sender[address][district]': 'centro',
'sender[address][city]': 'palmas',
'sender[address][state]': 'TO',
'sender[address][country]': 'BRA',
'sender[address][postalCode]': 'CEP',
'sender[documents][0][type]': 'CPF',
'sender[documents][0][value]': 'CPF',
'paymentMethod[type]': 'CREDITCARD',
'paymentMethod[creditCard][token]': 'b2f303ba63964404b6c466323deb9078',
'paymentMethod[creditCard][holder][name]': 'LUCINEIA',
'paymentMethod[creditCard][holder][birthDate]': '11/01/1990',
'paymentMethod[creditCard][holder][documents][0][type]': 'CPF',
'paymentMethod[creditCard][holder][documents][0][value]': 'CPF',
'paymentMethod[creditCard][holder][phone][areaCode]': '63',
'paymentMethod[creditCard][holder][phone][number]': '991047876' }
我认为一切正常,这是我接收到的JSON格式的邮件。我的代码有什么问题?
答案 0 :(得分:1)
尝试像这样发送您的请求:
adb connect <ip_address>:5555