I just want to connect my AngularJS app with a RESTFUL API in node.js, but i have problems getting the post content with body-parse.
This is my angularJS request by $Http. "user" is a json like this
{email: "juanfran@test.com", psw: "Juanfran"}
$scope.log = function(user){
console.log(user);
$http.post(APIURL + "/usuario/auth", user, {headers: {"Content-type": "application/x-www-form-urlencoded"}})
.then(function successCallback(response) {
if(response.data[0] != null){
$localStorage.token = response.data;
}
else{
swal({ title: "Error", text: response.data.Error, type: "error", confirmButtonText: "Ok" });
}
}, function(error){ swal({ title: "Error", text: error.data.Error, type: "error", confirmButtonText: "Ok" }); });
return false;
}
And this is my node.js configuration and route.
var methodOverride = require('method-override');
var app = express();
app.use(logger('dev'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
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");
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, PATCH, OPTIONS');
next();
});
And how I get the form data:
router.post("/usuario/auth/", function(req, res){
var userData = {
email : req.body.email,
password : req.query.pwd,
};
console.log(userData);
console.log(req.get("email"));
UserModel.authUsuario(userData,function(error, data)
{
if(error == null && data && data.status === "OK")
{
res.status(200).json({"Success":"Correcto."});
}
else
{
res.status(200).json({"Error":error.Error});
}
});
});
And the exit...
{ email: undefined, password: undefined }
undefined
POST /usuario/auth 200 4.668 ms - 32
And there are the POST headers
Request URL:http://localhost:3000/usuario/auth
Request Method:POST
Status Code:200 OK
Remote Address:[::1]:3000
Referrer Policy:no-referrer-when-downgrade
**Response Headers**
view source
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods:POST, GET, DELETE, PATCH, OPTIONS
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:32
Content-Type:application/json; charset=utf-8
Date:Mon, 12 Jun 2017 19:16:29 GMT
ETag:W/"20-VvY6+aRGMnbvwJDOEFg7V2+2T9E"
X-Powered-By:Express
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:es-ES,es;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:58
Content-type:application/x-www-form-urlencoded, application/x-www-form-urlencoded;charset=UTF-8;
Host:localhost:3000
Origin:http://localhost
Pragma:no-cache
Referer:http://localhost/juanfrantraining/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
I can not see what it's happening. Thanks!
via Hipólito Pérez
No comments:
Post a Comment