如何将数据从节点 js POST 到 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();


弑天下
浏览 149回答 1
1回答

慕婉清6462132

我找到了一个答案,我们必须将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 = {&nbsp; &nbsp; pos1: '10',&nbsp; &nbsp; pos2: '15'};var post_body_json = JSON.stringify(post_body);var post_options = {&nbsp; &nbsp; host: 'localhost',&nbsp; &nbsp; path: '/govtech/pab/branches/testing/index.php/websocket/node_data',&nbsp; &nbsp; body: post_body_json,&nbsp; &nbsp; method: 'GET',&nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; 'User-Agent': 'Super Agent/0.0.1',&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type': 'application/x-www-form-urlencoded',&nbsp; &nbsp; }};&nbsp;var post_req = http.request(post_options, function (res) {&nbsp; &nbsp; res.on('data', function (chunk) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('Response: ' + chunk);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; post_req.end();
打开App,查看更多内容
随时随地看视频慕课网APP