1

Possible duplicates:

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

Environment

  • 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

<?php
Class Welcome extends CI_Controller {

public function __construct(){
  parent::__construct();
  $this->load->helper('url');
}

public function index(){
  $this->load->view('welcome_view');
}

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

Model - Welcome_model.php

<?php
Class Welcome_model extends CI_Model
{
    public function __construct(){
      parent::__construct();
      $this->load->helper(array('form','url'));
      $this->load->database();
    }

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

      if($this->db->affected_rows()>0){
        $this->db->close();
        return 'true';
      }
      else{
        $this->db->close();
        return 'false';
      }
    }
}

view -> welcome_view.php

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

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">

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

  $(document).on('click','#btn-id',function(e){
    var testData = "12345";
    socket.emit('send_data', testData);
  });
</script>
</body>
</html>

Node file -> server.js

var socket  = require('socket.io');
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) {
  if(err){
    console.error(err);
    return;
  }
  $ = 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);
    $.ajax({
        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.?

ArunValaven
  • 1,753
  • 2
  • 25
  • 44
  • Possible duplicate of [How to make an HTTP POST request in node.js?](https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js) – Kukic Vladimir Jul 10 '17 at 13:24
  • @Kukic Vladimir .. Given link may related to my problem. But not solve my problem. Codeigniter controller not called and there is no data inserted into db. How to check the post call from server.js in browser. Command prompt showing return data for http post like http://google.com/. – ArunValaven Jul 10 '17 at 13:50
  • @Kukic Vladimir .. If you understand my issue, please give your code where i am going wrong.. – ArunValaven Jul 10 '17 at 13:51

1 Answers1

0

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('socket.io');
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);

    request.post({
      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

Kukic Vladimir
  • 1,010
  • 4
  • 15
  • 22
  • I tried your code.. It's showing only response in command prompt without any update in database. – ArunValaven Jul 11 '17 at 05:47
  • This is the image [link](https://ibb.co/eWfSfF) which shows response in command prompt... – ArunValaven Jul 11 '17 at 06:00
  • @ArunValaven meaning you have successfully reached the apache server, now you need to inspect this on `apache` server and align `post` parameters and url with one defined in php code. – Kukic Vladimir Jul 11 '17 at 07:55