2017-07-10 13:18
浏览 99


Possible duplicates:

I tried above and other links related to my problem. But not get any solution.


  • OS : Windows 7 - 64 bit
  • Framework : CodeIgniter PHP MVC
  • Client side: Javascript - Jquery
  • Server side: NodeJS
  • Database : MySQL

I need to make a http post call from server(Node + express)(server.js) to Codeigniter controller file.

I am using Jquery Ajax to make a post call in Node server.js file. But post call not working in client end.

Code Samples:

Controller - Welcome.php

Class Welcome extends CI_Controller {

public function __construct(){

public function index(){

function testFunction(){
  $getData = $this->input->post('sendData');
  $rec_data = $this->Welcome_model->testFunction($getData);
  echo json_encode($rec_data);

Model - Welcome_model.php

Class Welcome_model extends CI_Model
    public function __construct(){

    function testFunction($getData){
      $this->db->query("INSERT INTO test_tbl(test) VALUES('$getData')");

        return 'true';
        return 'false';

view -> welcome_view.php

<!DOCTYPE html>
<html lang="en">
    <title>view file</title>
  <div class="container">
      <button id="btn-id">Click me</button>

<script src=""></script>

<script type="text/javascript">

  var socket_link = "socket_link";
  var socket = io.connect(socket_link);

    var testData = "12345";
    socket.emit('send_data', testData);

Node file -> server.js

var socket  = require('');
var express = require('express');
var app     = express();
var server  = require('http').createServer(app);
var io      = socket.listen( server );
var port    = process.env.PORT || 3000;

var $;
require("jsdom/lib/old-api").env("", function(err, window) {
  $ = require("jquery")(window);

server.listen(port, function(){
  console.log('Server listening at port %d', port);

io.on('connection', function (socket){
  socket.on('send_data', function(data)
    console.log("test line: "+data);
        type: "POST",
        url: "http://ip:port/welcome/testFunction",
        data: "sendData="+data,
        dataType: "text",
        cache : false,
        success: function(getData){
          var parseData = $.parseJSON(getData);
          console.log("parseData: "+parseData);

Node server started in my project. Server listening the port. Onclick button in view, socket will emit in view and socket will receive the data in server.js file.

I need ajax post call i need to done in server.js, which will call controller-> model-> data will be inserted into database.

Need an return value in view through server.js file.

How to check post call in browser..? How to achieve this.?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupinwan0563 2017-07-10 14:03

    In order to make calls from node server to external server I have been using request module.

    Here is a snippet of the code how this could be achieved using request

    var socket  = require('');
    var express = require('express');
    var app     = express();
    var server  = require('http').createServer(app);
    var io      = socket.listen(server);
    var port    = process.env.PORT || 3000;
    var request = require('request');
    server.listen(port, function() {
      console.log('Server listening at port %d', port);
    io.on('connection', function(socket) {
      socket.on('send_data', function(data) {
        console.log("test line: " + data);
          url: 'http://ip:port/welcome/testFunction',
          data: data
        }, function optionalCallback(err, httpResponse, body) {
          if (err) {
            return console.error('upload failed:', err);
          console.log('Server responded with:', body);

    Note: This is not fully functional example as you might need to adjust post method parameters and headers like application/x-www-form-urlencoded or multipart/form-data,...

    Hope that this helps a bit

    解决 无用
    打赏 举报

相关推荐 更多相似问题