2016-08-25 31 views
0

私の角度jsファクトリでは、入力モデル値をサーバー側(ノードjs)に転送するためにajax呼び出しを行っていますが、取得しようとすると 'undefined' 。ノードjsの要求パラメータを取得できません

角度JS:私は以下有するノードJSで

angular.module('name-App').factory('myService', function($http,$rootScope) { 

    return { 

     getFoos: function(stock) { 
      console.log("----------->"+stock.toString());//displays the value correctly over here . 
      //return the promise directly. 
      return $http({ 
     url:'http://localhost:3000/gethistorydata', 
     method: "GET", 
     params: stock 
    }).then(function(result) { 


          alert("result.data"+result.data); 
          return result.data; 
         }).catch(function(fallback) { 
    alert("failed"+fallback + '!!'); 
    }); 
     } 
    } 
}); 

app.get('/gethistorydata',function(req,res){ 
console.log("--------------->>>>>>"); 

console.log("stock name = "+req.params.data); 
    //res.cookie(); 
//res.sendFile("F:/customer/public/index.html"); 
}); 

req.params.dataは未定義です。構文に何がありますか?

答えて

1

req.paramsは、urlパスパラメータを参照します。だから、サポートするためにあなたのルートに1つを追加する必要があります。たとえばこの経路定義(エンドに追加/:idに注意してください)と/gethistorydata/123への要求:

app.get('/gethistorydata/:id, function(req, res) { 
    console.log('my id is ' + req.params.id'); 
}); 

は「私のIDが123である」ログであろう。

私はあなたがreq.queryを探していると思います。あなたのstockオブジェクトはどのように見えますか?それはのように見えた場合、角度この{"price": 123}は、要求が/gethistorydata?price=123見えるように修正します、あなたは、このようにコードを変更することができます:

多くのためのドキュメントを参照してください「私の株価は123である」ログう

app.get('/gethistorydata, function(req, res) { 
    console.log('my stock price is ' + req.query.price'); 
}); 

を例http://expressjs.com/en/api.html#req.query

+0

私のparamはparamsと似ています:{st:stock}ここで、在庫はnse:storeoneの形式ですが、これはconsole.http:// localhost:3000/gethistorydata?st = nse:storeone 404(私が "nse:storeone"をセミコロンが無視されないように単一の値としてどのように送るべきですか? –

関連する問題