【问题标题】:How do i extract the value of params object in express.js http get request如何在 express.js http get 请求中提取 params 对象的值
【发布时间】:2017-04-16 03:19:36
【问题描述】:

所以我在angular js的工厂服务中提出了一个get请求

.factory('FollowUpTodaySvc', function($q, _, $http, currentUrl, TodayDateSvc) {
        var deferred_FTS = $q.defer();
    var getFollowUp = function(d) {
    // var getFollowUp = function(ob) {
var ob = {};
ob.a = "a";
ob.b = "b";
var url = currentUrl+"/api/FollowUpSearch/"+ob;           

      $http.get(url)
            .success(function(jsonData_FTS) {
console.log('%c 518-S getFollowup json Data =', 'background: white; color: red',jsonData_FTS);
                deferred_FTS.resolve(jsonData_FTS);
            })
            .error(function(error_FTS) {

当此请求发送到 express 服务器时,我无法从 params 对象中提取值。

这是我的服务器代码。

exports.FollowUpSearch = function(req, res, next, id) {
console.log('224-psc FollowUpSearch-API  id = ', id);
console.log('397-psc FollowUpSearch-API  req.params = ', req.params);
console.log(chalk.green('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%:     ' + "&&&&&&&&&"));

这里是终端截图。

第二次尝试发送数据

$http.get(url, obj)

这里的 id 未定义。

场景 2。 服务中:

 var  data = {usernameOrEmail: "some email ", password: "PAssword"};

         console.log('513-S---- inside FollowUpTodaySvc ');
           var url = currentUrl+"/api/FollowUpSearch/"+d;
           // var url = currentUrl+"/api/FollowUpSearch/"+d;           
        $http.get(url, data )

在服务器上

console.log('403-psc FollowUpSearch-API  req.params = ', req.params);
console.log('403-psc FollowUpSearch-API  req.body = ', req.body);

场景 3。

将 $http 从 get 更改为 post 解决了它,我可以使用 req.body 对象获取值,但我的问题是如果我不想发出 Post 请求怎么办,我只想获取它可以缓存。 我如何发送数据后端?

场景 4 - 字符串化

exports.Later_Today_P_prioritySearch = function(req, res, next, id) {

  console.log(chalk.white('354-PSC - - Environment:  inside  Later_Today_P_prioritySearch  '));

console.log(chalk.white('356-PSC - - req.body  ', req.body));
var stri_body = JSON.stringify(req.body)
console.log(chalk.white('358-PSC - - stri_body   ', stri_body));


console.log(chalk.white('356-PSC - - req.params  ', req.params));

var stringy = JSON.stringify(req.params)

console.log(chalk.white('356-PSC - - stringy   ', stringy));

console.log(chalk.white('356-PSC - - stringy Later_Today_P   ', stringy.Later_Today_P));

有趣的是它适用于 req.params 但不适用于 req.body

这是角度服务

  var deferred_la_P = $q.defer();

    var getPriority = function(p) {
          url = currentUrl+"/api/Later_Today_P_prioritySearch/"+p;

      console.log('880-S  p =', p)

var data = { username : '$localStorage.username', email: '$localStorage.email' };

        $http.get(url, data)

场景:5(使用http.post进行字符串化

与上面相同的代码。

$http.post(url, data)

场景6:

使用 $http 发布所有这些字符串都是不必要的

因为我可以使用以下方法访问该值。

console.log(chalk.white('364-PSC - - req.body.username ', req.body.username));

但问题仍然存在:如何通过 http get 请求发送数据对象。

【问题讨论】:

    标签: angularjs http express meanjs


    【解决方案1】:

    你能在你的日志语句中使用吗

    console.log('224-psc FollowUpSearch-API  id = ',JSON.stringify(id));
    console.log('397-psc FollowUpSearch-API  req.params = ', JSON.stringify(req.params));
    

    您应该通过这种方式获得该对象的更多信息,而不仅仅是类型。

    【讨论】:

      【解决方案2】:

      您的第一张图片显示您的request.params 有东西。

      无论如何,根据the documentation$http.get 的第二个参数是一个配置对象。配置对象现在可以具有参数。所以你应该改变你的代码如下:

      $http.get(url, {params:obj})

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2016-09-17
        • 1970-01-01
        • 2012-11-20
        • 2019-11-24
        相关资源
        最近更新 更多