使用nodejs的post请求不会进入$ _GET

时间:2014-12-15 21:36:59

标签: php node.js

首先,PHP脚本:

error_log("inside the script"); //this is logged

if(isset($_GET['test'])){
    echo "test";
    echo json_encode("test");
}

nodejs位

var postToPHP = function(data, path, method_type){

var querystring = require("querystring");
data = querystring.stringify(data);

console.log("stringified data:",data);

var options = {
    host : 'localhost',
    path : path,
    method : method_type,
    headers : {
        'Content-Type' : 'application/x-www-form-urlencoded',
        'Content-Length' : data.length
    }
};

var buffer = "";

var http_client = require('http');
var reqPost = http_client.request(options, function(res) {

    res.on('data', function(d) {
            buffer = buffer+d;
        });
    res.on('end', function() {
           console.log("buffer",buffer);
         });

reqPost.write(data);
reqPost.end();
}

postToPHP("test","/test.php","GET");
postToPHP({"test":"null"},"/test.php","GET");

我无法理解为什么会发生这种情况,因为我之前确实有过这种情况。然后我改变了一些我无法理解的东西,它停止了工作......对这个有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试设置解析为$ _GET的查询参数!

postToPHP("test","/test.php?test=1","GET");
postToPHP({"test":"null"},"/test.php?test=1","GET");
相关问题