2015-07-10 112 views
0

我有一个JS文件管理我的数据(推送我的JSON对象中的数据等),以及来自CodeIgniter的经典MVC结构文件。CodeIgniter和JSON对象:如何将数据推送到数据库?

我的JS包含我想推入我的数据库的JSON对象。我该怎么办?我怎样才能从我的JS文件到达控制器和模型?我无法弄清楚什么是实现我的目标的正确过程!我发现没有类似于我的问题。

编辑 的推到数据库中的数据是整个JSON object.The数据的一部分来推动,例如:{ "index": 0, "x": 50, "y": 80, "weight": 2, "px": 50, "py": 80, "fixed": 0 } 在我的JS文件,我已经试过这段代码:

$("#hexa-btn").on("click", function() { 

    $.ajax({ 
     type: "POST", 
     url: "/prototype/returndata", 
     data: JSONshapes.shapes[0].nodes[0], 
     cache: false, 
     success: 
      function(data){ 
       console.log(data.index); 
       console.log(data.x); 
       console.log(data.y); 
      } 
      }); 
    }); 

我的控制器具有这样的功能:

function returndata(){ 
    $index = $this->input->post('index'); 
    $x = $this->input->post('x'); 
    $y = $this->input->post('y'); 
    $weight = $this->input->post('weight'); 
    $px = $this->input->post('px'); 
    $py = $this->input->post('py'); 
    $fixed = $this->input->post('fixed'); ; 

    echo json_encode(array('node'=>$node)); 
    } 

我不知道在所有关于这个功能。看起来这是模型的角色来完成这项工作,不是吗?

第二编辑所以,我尝试了@Harish Lalwani的解决方案,但这次与我的节点阵列(不只一个)。我有JavaScript文件中有以下功能:

function sendNode(){ 
    var node_url = "/prototype/insert_node"; 
    var data_node = JSON.stringify(JSONshapes.shapes[0].nodes); 
    $.post(node_url, {'node_data': data_node}, function(data){ 
     console.log(data.index); 
    }); 
    } 

,并在控制器下面的一个(感谢到this post):

function insert_node(){ 
     $node_data = $this->input->post('node_data'); 
     $node_data = json_decode($node_data,true); 
     echo 'Your Data: ' . $node_data[0]['index']; 
    }, 

但是,打印数据的时候,我得到undefined。变量data_node是下面的(所以,是一个数组):

[{"index":0,"x":50,"y":80,"weight":2,"px":50,"py":80,"fixed":0},{"index":1,"x":189,"y":107,"weight":2,"px":189,"py":107},{"index":2,"x":95,"y":145,"weight":2,"px":95,"py":145}] 

现在,我不知道了该怎么办!我发现真的太少例子。任何人都可以让我摆脱苦难吗?非常感谢您提前!

+1

使用$ .ajax.post –

+1

显示你的代码,你尝试过什么? – CodeGodie

回答

0

使用ajax可以解决您的问题!

$.ajax({ 
     type: "POST", 
     url: "test.php", 
     data: yourData, 
     dataType: "text", 
     cache:false, 
     success: 
       function(data){ 
       alert(data); //as a debugging message. 
       } 
      });// you have m 
+0

谢谢你的帮助!我尝试了你的解决方案,但我现在只得到“未定义”的结果。我继续尝试。 – Auri

0

从您的Js文件中触发此操作。 (包括jQuery库)

提供网址,发布键和值,您将在指定的网址上接收发布参数。

 jQuery.post("<URL>", {dataname: datavalue}, function(r) { 
       console.log(r); 
      }); 

我也使用ajax。

+0

谢谢你的建议!我不知道这种方法,但我会搜索并阅读关于它的信息。 – Auri

0

从您的原始帖子,我看到你有一个JSON对象。如果它已经是对象形式,则根本没有必要转换它。只是,JSON分配给一个变量,该变量传递给你的Ajax data参数,像这样:

$("#hexa-btn").on("click", function() { 

    var json = { "index": 0, "x": 50, "y": 80, "weight": 2, "px": 50, "py": 80, "fixed": 0 }; 

    $.ajax({ 
     type: "POST", 
     url: "/prototype/returndata", 
     data: json, 
     success: function(data){ 
      console.log(data.index); 
      console.log(data.x); 
      console.log(data.y); 
     } 
    }); 

}); 
+0

谢谢你的帮助。但我不确定明白你的意思。我在哪里可以在我的代码中进行转换?那么,我需要在'returndata()'函数中写入什么? – Auri

+0

没问题。我想我以为你正在运行'JSONshapes.shapes [0] .nodes [0]'将一些数据转换为对象形式,在那里你说你已经有了JSON ......在'returndata()'中,你可以做任何你喜欢的事情。你想要它执行什么功能?到目前为止,它只是抓取从ajax – CodeGodie

+0

发送的数据,这是假设要做的'JSONshapes.shapes [0] .nodes [0]'是什么? – CodeGodie

相关问题