我目前正在尝试设置EasyPost API,我需要存储来自POST路由的响应中包含的一些信息。我想将响应中的数据直接发送到客户端进行存储,但我无法找到发送信息的位置或方式。
这是我收到信息的表格:
<div ng-controller="easypostController as epc" class="col l6">
<!-- -->
<h2>Customer address</h2>
<form class="blue-grey darken-4">
<input ng-model="epc.address.name" placeholder="Name">
<input ng-model="epc.address.street1" placeholder="Street 1">
<input ng-model="epc.address.street2" placeholder="Street 2">
<input ng-model="epc.address.city" placeholder=" City">
<input ng-model="epc.address.state" placeholder="State">
<input ng-model="epc.address.zip" placeholder="Zip">
<input ng-model="epc.address.country" placeholder="Country">
<input ng-model="epc.address.phone" placeholder="Phone">
<a class="waves-effect waves-light btn" ng-click="epc.sendAddress()">Verify</a>
</form>
</div>
这是控制器将信息从表单传递到工厂:
function easypostController(easypostFactory) {
var epc = this
epc.address = {}
epc.sendAddress = function() {
epc.resId = {}
easypostFactory.send(epc.address)
.then(function(res) {
console.log("Successfullly sent address epc")
// epc.resId = easypostFactory.resId
epc.address = {}
})
// console.log(epc.resId)
}
}
这是我的工厂,它将信息传递给服务器:
address.send = function(address) {
return $http.post(addressUrl, address)
// epf.resId = address
console.log(address, "=====")
}
这就是我遇到问题的地方,我无法获得任何res.send()函数将响应对象发送回客户端。
var address = easypost.Address.create(req.body, function(err, fromAddress) {
var verifiedAddress = {}
fromAddress.verify(function(err, response) {
console.log("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
if (err) {
console.log('Address is invalid.')
} else if (response.message !== undefined && response.message != null) {
console.log('Address is valid but has an issue:', response.message)
var verifiedAddress = response.address
} else {
verifiedAddress = response.address
console.log(verifiedAddress)
res.send(verifiedAddress)
}
})
})
VerifiedAddress是我的服务器记录的正确对象我无法将响应对象发送到客户端。任何帮助将不胜感激。
由于
答案 0 :(得分:0)
而不是使用角度js作为前端,尝试使用简单的html表单。这是一个例子:
<form name="simple" action="someurl" method="post"> <input type="text" name="username"> </form>
您可以通过以下代码获取输入文本的值:
router.post("someurl", function(req,res){ var username = req.body.username }) console.log(username)
希望这会对你有所帮助:)。