dongnanbi4942
2017-07-10 13:18
浏览 99

Ajax在Codeigniter中从节点服务器调用到控制器,数据未插入数据库

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.?

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

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('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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题