angularjs - req.body.stripeToken is undefined -


i'm trying use $http post form nodejs server. when post, go server , try variable stripetoken using req.body.stripetoken, returns undefined.

when try post request using method="post" , action="http://localhost:3000/api/posts" attributes on form in html file, works charm. reason believe server side code ok, there wrong $http request.

i've read several posts problem, have yet see working solution me.

solutions other posts:

  • define configurations before define routes in server file.
  • add body-parser middleware in server file.
  • define headers http request.

i have tried these solutions, none worked. ofcourse can use method works, since i'm using angularjs using $http.

$http post request:

        $http({             method: 'post',             url: 'http://localhost:3000/api/posts',             data: form.get(0),             headers: { 'content-type': 'application/x-www-form-urlencoded' }         }).             then(function (response) {                 console.log(response.data);             }); 

what i'm trying send (form.get(0)):

<form id="payment-form" ng-controller="paymentcontroller" class="ng-pristine ng-valid ng-scope">     <span class="payment-errors"></span>      <div class="form-row">         <label>             <span>card number</span>             <input type="text" size="20" data-stripe="number">         </label>     </div>      <div class="form-row">         <label>             <span>expiration (mm/yy)</span>             <input type="text" size="2" data-stripe="exp_month">         </label>         <span> / </span>         <input type="text" size="2" data-stripe="exp_year">     </div>      <div class="form-row">         <label>             <span>cvc</span>             <input type="text" size="4" data-stripe="cvc">         </label>     </div>      <button type="submit" ng-click="generatetoken()" disabled="">buy now</button> <input type="hidden" name="stripetoken" value="tok_18g8jbefg6wb0fqnyyibxjwf"></form> 

server code:

var express = require("express"); var bodyparser = require("body-parser"); var stripe = require("stripe")("secret code");   var app = express();  app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true }));  app.use(function (req, res, next) {     res.header("access-control-allow-origin", "*");     res.header("access-control-allow-headers", "origin, x-requested-with, content-type, accept"); next(); });  app.listen(3000);  app.post("/api/posts", function (req, res) {     console.log('in post--server');     var stripetoken = req.body.stripetoken;     console.log(stripetoken);     var charge = stripe.charges.create({         amount: 1000, // amount in cents         currency: "eur",         source: stripetoken,         description: "example charge"     }, function (err, charge) {         if (err && err.type === 'stripecarderror') {             res.send("the card has been declined");         } else res.send("transaction completed");     }); }); 

console.log(req.body); gives following answer:

{ '{"0":{},"1":{},"2":{},"3":{},"4":{"jquery221039077944120836341":{"events":{"click":': {   '{"type":"click","origtype":"click","guid":3,"namespace":""}': { '{"type":"$destroy","origtype":"$destroy","guid":1,"namespace":""},{"type":"$destroy","origtype":"$destroy","guid":2,"namespace":""}': '' } } } 

i tried looking answer, found none. please forgive me if duplicate question.

below implementation should work, form.get(0) returns jquery event, not form values

$http({     method: 'post',     url: 'http://localhost:3000/api/posts',     data: $('#payment-form').serialize(),     headers: { 'content-type': 'application/x-www-form-urlencoded' } }).then(function (response) {         console.log(response.data); }); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -