node.js - CORS enabled - ajax response failure -
i trying make ajax 'post' request backbone client node.js server. have enabled cors on server seems successful not getting 'allow-cross-origin' errors in console' , server receiving posted data - posting username / password client , searching match in user database - server responds username found receiving ajax error on client. can soemone please - has been killing me days
here client user model:
app.userauth = backbone.model.extend({ defaults : { username : '', password : '' }, initialize : function(){ this.testreq() }, testreq : function(){ $.ajax({ url : "http://localhost:8080/api/authenticate", method : "post", datatype : "json", xhrfields : { withcredentials : true }, data : { username:"bottles24", password:"yoyo1234" }, success : function(data){ console.log(data); }, error : function(thrownerror){ console.log(thrownerror) } }); } });
and here server.js file being run on different port client:
var express = require('express'), app = express(), bodyparser = require('body-parser'), morgan = require('morgan'), mongoose = require('mongoose'), uriutil = require('mongodb-uri'), jwt = require('jsonwebtoken'), config = require('./config'), user = require('./models/user'); // enable cors var allowcrossdomain = function(req, res, next) { res.header('access-control-allow-origin', 'http://localhost:4000'); res.header('access-control-allow-methods', 'get,put,post,delete,options'); res.header('access-control-allow-headers', 'content-type, authorization, content-length, x-requested-with'); res.header('access-control-allow-credentials', 'true'); // intercept options method if ('options' == req.method) { res.send(200); } else { next(); } }; var port = process.env.port || 8080; var mongodburi = config.database; var mongooseuri = uriutil.formatmongoose(mongodburi); mongoose.connect(mongooseuri); app.set('supersecret', config.secret); app.use(bodyparser.urlencoded({ extended : false })); app.use(bodyparser.json()); app.use(morgan('dev')); // instance of router api routes var apiroutes = express.router(); apiroutes.post('/authenticate', function(req, res){ console.log(req.body) // find user user.findone({ username : req.body.username }, function(err, user){ if (err) throw err; if(!user){ res.json({ success: false, message : 'authentication failed'}) } else if(user){ // check password if (user.password != req.body.password){ res.json({success : false, message : 'authentication failed. wrong password'}) } else{ // if user found , password right // create token var token = jwt.sign(user, app.get('supersecret'), { expiresinminutes : 1440 // expires in 24 hours }); // return info including token json res.json({ success : true, message : 'enjoy token', token : token }); console.log('user found') } } }) }); // apply routes application prefix /api app.use('/api', apiroutes); // start server app.listen(port); console.log('listening @ port: '+ port)
when initialize ajax request - server logs 'user found' reason don't receive client - when test server code using 'postman' works fine i'm assuming on front-end... can see issue is? appreciated. thanks
edited: have got working, reason ajax request still fails when url 'localhost:4000' (which domain specified in access-control-allow-origin) - when url 'localhost:4000/?#' request succesful - know why might be?
Comments
Post a Comment