简体   繁体   English

$ .post()数据到Codeigniter 3

[英]$.post() data to Codeigniter 3

I have an $.post() and i want receive the data into my controller, how could i do that? 我有一个$.post() ,我想将数据接收到我的控制器中,我该怎么做?

jQuery jQuery的

$.post($('#url').val() + "Dashboard/getApi", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

vvvvvvvvvvvvvvvvvvv this is my url vvvvvvvvvvvvvv
$('#url').val() + "Dashboard/getApi" = "http://127.0.0.1/M2MWare/Dashboard/getApi"

And this is my controller 这是我的控制器

function getApi()
    {
    $valores = $this->input->post();
    // print_r($valores);
    return json_encode($valores);
    }

this returns me an empty array, i tried with different url, data and nothing why? 这返回了一个空数组,我尝试使用不同的url,数据,但是为什么呢?

Codeigniter 3.1.0 running on WampServer. 在WampServer上运行的Codeigniter 3.1.0。

The Dashboard Controller 仪表板控制器

<?php
defined('BASEPATH') OR exit( 'No direct script access allowed' );

class Dashboard extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

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

    function getApi() {
        $valores = $this->input->post();
        echo json_encode($valores);
    }
}

The View post_view.php This is Very Bare Bones for simplicity. 视图post_view.php为简单起见,这是非常裸露的骨头。

<html lang="en">
<head>
<title>Post Me</title>
</head>
<body>
<form>
    <input type="hidden" id="url" value="http://ci310tut.com/">
</form>

<script src="<?= base_url('assets/js/jquery.min.js'); ?>"></script>
<script>
    $(document).ready(function () {
        console.log('We should be seeing an alert popup');
        $.post($('#url').val() + "Dashboard/getApi", {name: "John", time: "2pm"})
            .done(function (data) {
                alert("Data Loaded: " + data);
            });
    });
</script>
</body>
</html>

And from the above I get an alert screaming at me each time I perform a page refresh with the provided data being displayed. 从以上内容可以看到,每次执行页面刷新并显示提供的数据时,我都会大叫一声。

So you might want to check the above against what you have. 因此,您可能想要对照所拥有的内容检查以上内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM