I got the following code from here
我已尝试使用变量data
等于'test=yes'
或data
等于{test:'yes'}
这是php脚本
if(isset($_POST['test'])){
error_log("inside");
echo "and I'd also like to return this";
};
这是 nodejs 代码片段:
function postToPHP(data, path){
var httpreq = require('http');
var querystring = require("querystring");
var data = querystring.stringify(data);
var options = {
host : 'localhost',
path : 'www' + path, //path is well defined in the actual code
method : 'POST',
headers : {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : data.length
}
};
var buffer = "";
var reqPost = httpreq.request(options, function(res) {
res.on('data', function(d) {
buffer = buffer+data;
});
res.on('end', function() {
return buffer;
});
});
reqPost.write(data);
reqPost.end();
}
postToPhp的调用
//treat message?
var message = "test=yes";
//OR
var message = "{test:'yes'}";
var buffer = postToPHP(message,"path");
console.log("buffer from PHP",buffer);
buffer
未定义
错误日志中没有显示任何内容,我认为某些内容无法使用我无法解释的代码,所以我希望有人可以帮我解决这个问题。