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

douli6605
douli6605 弗拉基米尔..如果您了解我的问题,请将您的代码告诉我出错的地方..
3 年多之前 回复
dpfl37651
dpfl37651 弗拉基米尔..鉴于链接可能与我的问题有关。但不能解决我的问题。没有调用Codeigniter控制器,并且没有数据插入到db中。如何在浏览器中检查来自server.js的帖子调用。命令提示符显示google.com等http帖子的返回数据。
3 年多之前 回复
douchuanchai2793
douchuanchai2793 可能重复如何在node.js中发出HTTPPOST请求?
3 年多之前 回复

1个回答



为了从节点服务器调用外部服务器,我一直在使用 request module。</ p>

以下是代码片段,如何使用 request </ code> </ p>

  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('服务器侦听端口%d',端口);
}); \ n
io.on('connection',function(socket){
socket.on('send_data',function(data){
console.log(“test line:”+ data);

请求 .post({
url:'http:// ip:port / welcome / testFunction',
data:data
},function optionalCallback(err,httpResponse,body){
if(err){
return console.error('upload failed:',err);
}
console.log('服务器响应:',body);
});
});
});
</ code> </ pre>

注意:</ strong>这不是完全正常的示例,因为您可能需要调整 post </ code>方法参数和标题,如< 代码> application / x-www-form-urlencoded </ code>或 multipart / form-data </ code>,... </ p>

希望这有点帮助< / p>
</ div>

展开原文

原文

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

dtrt2368
dtrt2368 意味着你已成功到达apache服务器,现在你需要在apache服务器上检查这个并将post参数和url与php代码中定义的一个对齐。
3 年多之前 回复
douzhechi2435
douzhechi2435 这是在命令提示符中显示响应的图像链接...
3 年多之前 回复
dongyou9721
dongyou9721 我尝试了你的代码..它只显示命令提示符中的响应而没有在数据库中进行任何更新。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问