【问题标题】:How to pass information from a POST response back to the client NodeJS/ExpressJS如何将来自 POST 响应的信息传递回客户端 NodeJS/ExpressJS
【发布时间】:2016-11-04 01:54:18
【问题描述】:

我目前正在尝试设置 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 是我的服务器记录的正确对象我只是无法将响应对象发送到客户端。任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: angularjs node.js http express


    【解决方案1】:

    前端不要使用 Angular 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)
    

    希望对你有帮助:)

    【讨论】:

    • Wilidan 感谢您的回复,但我的问题不在于将信息从客户端传递到服务器,而是在我从我的 api POST 调用得到响应后将信息传递回客户端。我目前的解决方案是将 POST 响应存储在数据库中,然后从数据库中访问信息。感谢您的回复!
    猜你喜欢
    • 2014-05-03
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2021-10-20
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多