meteor:从NodeJs到PHP的jQuery ajaxpost
我正在使用这个 API在我的meteor应用程序中执行CRUD数据库操作。 到现在为止,每个操作都很好地完成。
现在考虑一个用例,其中发生了一些错误(例如一些validation错误,如无效参数),那么我想通过jQuery ajax调用将错误对象发送到php脚本。
所以问题是:如何从NodeJs脚本将对象发布到php?
我曾尝试通过meteor add jquery
添加Meteor的默认jquery包。 然后做了这样的事情:
var $ = require('jquery'); .... .... function test(data){ console.log('test'+JSON.stringify(data)); $.post({ type: 'POST', url: 'http://vibsy.com/test/chest.php', data: data, success: function(data) { console.log(data); } }); }
但是这显示错误:
ReferenceError: require is not defined at app\server\collectionapi.js:1:10
有任何想法吗?
jquery从客户端运行良好,即时通讯不太确定它将在服务器端工作。 如果你执行meteor add jquery
你不必使用require
。 meteor会自动引用准备使用的正确的js文件。 删除下面的行(仅客户端),它应该是好的去
var $ = require('jquery');
如果你的脚本在服务器上运行,看起来是这样的。 它更好地使用Meteor.http 。 运行meteor add http
并使用类似的东西:
Meteor.http.call("POST", "http://vibsy.com/test/chest.php",{params: data}, function (error, result) { if (result.statusCode === 200) { console.log(result); } });
或更舒适(同步):
var result = Meteor.http.call("POST", "http://vibsy.com/test/chest.php",{params: data}); if (result.statusCode === 200) { console.log(result.data); //JSON content console.log(result.content); //raw content }