2016-02-29 113 views
0

我想使用jQuery发布post请求到mongodb。运行在app localhost:3000上的 。 使用猫鼬我可以利用显示的电影:ajax后与jquery mongodb

router.get('/', function(req, res, next) { 
    mongoose.model('Movie').find(function(err, titles){ 
     res.render('testdb', { title: 'MYMusic', 
     className: 'index', 
     names: titles 
     }); 
     console.log('names: ', titles); 
    }); 
    }); 

但是,当我要动态地发布到使用jQuery使用我的数据库:

$('.save-video-button').on('click', function(event) { 

     var obj = { 
     title: 'some title' 
     }; 

     var URL = 'mongodb://localhost/mydatabase'; 

     $.ajax({ 
     url: URL, 
     type: "POST", 
     data: JSON.stringify(obj), 
     contentType: "application/json", 
     success: function(data) { 
      console.log('success --> data :', data); 

     }, 
     error: function(xhr, text, err) { 
      console.log('error: ',err); 
      console.log('text: ', text); 
      console.log('xhr: ',xhr); 
      console.log("there is a problem whit your request, please check ajax request"); 
     } 
     }); 

    }); 

当我运行这个Ajax请求得到它显示错误在谷歌C​​hrome开发工具控制台:

XMLHttpRequest无法加载mongodb:// localhost/mydatabase。协议方案仅支持交叉源请求:http,data,chrome,chrome-extension,https,chrome-extension-resource。

如何避免此交叉原点请求?

谢谢!

回答

1

好的我明白了!任何人同样的问题,不要忘记有一个router.post在你的服务器端:

router.post('/', function(req, res){ 
    var newmovie = res.body; 
    console.log(newmovie); 
});  

所以当你拨打电话,你其实可以通过数据=资源。