I am using this API to perform CRUD database operations in my meteor application. Till now, every operation is performed nicely.
Now consider a use case where some error occurs (eg. some validation error like invalid parameter) then I want to send the error object to the php script through jQuery ajax call.
So the question is: How to post the object to php from NodeJs script?
I have tried adding Meteor's default jquery package by meteor add jquery. and then done something like this:
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);
}
});
}
But this shows the error:
ReferenceError: require is not defined
at app\server\collectionapi.js:1:10
Any ideas?
Jquery works well from the client side, im not too sure it will work on the server end. If you performed meteor add jquery you don't have to use require. Meteor will automatically reference the correct js files ready to use. Remove the line below (client only) and it should be good to go
var $ = require('jquery');
If your script is running on the server, which it looks like it is. Its better to use Meteor.http. Run meteor add http and use something like:
Meteor.http.call("POST", "http://vibsy.com/test/chest.php",{params: data}, function (error, result) {
if (result.statusCode === 200) {
console.log(result);
}
});
Or more comfortably (sync):
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
}