node.js - POST to express.js with 'Access-Control-Allow-Origin' -


i'm newbie node , express. can data server app fail on post.

i have client side on "http://myapp.com" , node app on "http://myapp.com:34627".

this node app:

var express = require('express'), app = express(); var result = {};   app.configure(function() {     app.use(express.json());     app.use(express.urlencoded()); });  app.set("jsonp callback", true);   app.get('/get/users/:userid/:username',function(req,res) {     var userid = req.params.userid;     var username = req.params.username;      result = {         response: 'ok',         url_userid: userid,         url_username: username     };      res.header('access-control-allow-origin','http://listasapp.in');     res.json(200,result); });   app.post('/post/user',function(req,res) {     console.log( req.body );      result = {         response: 'ok'     };      res.jsonp(200,result); });   var server = app.listen(34627,function() {     console.log( 'server running @ http://myapp.com:' + server.address().port ); });   $('#get-data').on('click tap',function() {     var url = 'http://listasapp.in:34627/get/users';     url += '/24601';     url += '/junihh';      $.getjson(url,function(rsp)     {         console.log( rsp );     }); }); 

and client side:

$('#get-data').on('click tap',function() {     var url = 'http://myapp.com:34627/get/users';     url += '/24601';     url += '/junihh';      $.getjson(url,function(rsp)     {         console.log( rsp );     }); });  $('#post-data').on('click tap',function() {     var dta = {         userid: '24601',         username: 'junihh'     };      $.ajax({         type: 'post',         url: 'http://myapp.com:34627/post/user?callback=?',         data: dta,         datatype: 'jsonp',         success: function(rsp)         {                 console.log( rsp );         },         error: function(jqxhr,textstatus,errorthrown)         {                 console.log( jqxhr.status );                 console.log( textstatus );                 console.log( errorthrown );         }     }); }); 

i have successful request post error:

get http://myapp.com:34627/post/user?callback=jquery1110009652853850275278_1397958835486&userid=24601&username=junihh&_=1397958835487 404 (not found) 

i'm stuck here, don't figure out how fix error. wrong code ??

thanks or advise.

finally found answer post: https://github.com/visionmedia/express/blob/master/examples/cors/index.js

i add portion of code node app:

app.all('*',function(req,res,next) {     if (!req.get('origin')) return next();      res.set('access-control-allow-origin','http://myapp.com');     res.set('access-control-allow-methods','get,post');     res.set('access-control-allow-headers','x-requested-with,content-type');      if ('options' == req.method) return res.send(200);      next(); }); 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -