如何将数据从节点js发布到codeigniter?

时间:2019-08-01 11:14:51

标签: javascript php node.js codeigniter

我正尝试将数据从node.js发送到PHP codeigniter,以在我的项目中实现,我使用下面给出的代码尝试了此操作,但是codeigniter函数未接收到数据,有人可以帮我吗?谢谢

我的codeigniter控制器功能:

$_POST = json_decode(file_get_contents('php://input'), true);
echo "<pre>"; print_r($_POST);  

我的节点js代码:

var request = require('request');
var http = require('http');
var post_body = {
    pos1: '10',
    pos2: '15'
};
var post_body_json = JSON.stringify(post_body);
var post_options = {
    host: 'localhost',
    path: '/govtech/pab/branches/testing/index.php/websocket/node_data',
    body: post_body_json,
    method: 'POST',
    headers: {
        'User-Agent': 'Super Agent/0.0.1',
        'Content-Type': 'application/x-www-form-urlencoded',
    }
}; 
var post_req = http.request(post_options, function (res) {
    res.on('data', function (chunk) {
        console.log('Response: ' + chunk);
        });
    });

    post_req.end();

1 个答案:

答案 0 :(得分:0)

我找到了一个答案,我们必须将POST方法更改为GET方法。

更新的控制器代码:

$_POST = json_decode(file_get_contents('php://input'), true);
echo "<pre>"; print_r($_GET);

更新的节点js代码:

var request = require('request');
var http = require('http');
var post_body = {
    pos1: '10',
    pos2: '15'
};
var post_body_json = JSON.stringify(post_body);
var post_options = {
    host: 'localhost',
    path: '/govtech/pab/branches/testing/index.php/websocket/node_data',
    body: post_body_json,
    method: 'GET',
    headers: {
        'User-Agent': 'Super Agent/0.0.1',
        'Content-Type': 'application/x-www-form-urlencoded',
    }
}; 
var post_req = http.request(post_options, function (res) {
    res.on('data', function (chunk) {
        console.log('Response: ' + chunk);
        });
    });

    post_req.end();