简体   繁体   English

AJAX POST 到 CodeIgniter 控制器

[英]AJAX POST to CodeIgniter controller

I'm having some issues trying to post data to CodeIgniter i have this.我在尝试将数据发布到 CodeIgniter 时遇到了一些问题,我有这个。

Ajax阿贾克斯

$.ajax({
            url: $('#url').val() + "Dashboard/Index",
            dataType: 'json',
            contentType: 'application/json',
            type: 'post',
            data: {data: data},
            success: function( data ){
              console.log(data);
            },
            error: function( jqXhr, textStatus, errorThrown ){
              console.log(jqXhr);
              console.log(textStatus);
              console.log( errorThrown );
            }
        });

Controller |控制器 | Dashboard.php -> class: Dashboard, function: index Dashboard.php -> 类:仪表板,功能:索引

atm i'm trying with this only to see if something its posting, but no! atm 我只是想看看它是否发布了一些东西,但没有!

if($_POST) {
            print_r($_POST);
            die();
        }

So my whole controller looks like this...所以我的整个控制器看起来像这样......

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

class Dashboard extends CI_Controller 
    public function index()
    {

        // $test = $_POST['data'];
        // echo json_encode($_POST);
        if($_POST) {
            print_r($_POST);
            die();
        }


//////////////////////////////////Views///////////////////////////////// 



    // includes
    $data['footer'] = $this->load->view('includes/footer', NULL, TRUE);

    // App
    $data['menuProfile'] = $this->load->view('app/menuProfile', NULL, TRUE);
    $data['sidebarMenu'] = $this->load->view('app/sidebarMenu', NULL, TRUE);
    $data['topNavigation'] = $this->load->view('app/topNavigation', NULL, TRUE);
    $data['menuFooter'] = $this->load->view('app/menuFooter', NULL, TRUE);

    // Dashboard

    $dashboard['dinp'] = $this->load->view('app/dashboard/dinp', NULL, TRUE);
    $dashboard['dispTitle'] = $this->load->view('app/dashboard/dispTitle', NULL, TRUE);
    $dashboard['porcentaje'] = $this->load->view('app/dashboard/porcentaje', NULL, TRUE);
    $dashboard['temp'] = $this->load->view('app/dashboard/temp', NULL, TRUE);
    $dashboard['velocity'] = $this->load->view('app/dashboard/velocity', NULL, TRUE);
    $data['dashboard'] = $this->load->view('app/dashboard/dashboard', $dashboard, TRUE);


    $this->load->view('includes/head');
        $this->load->view('app', $data);
    $this->load->view('includes/jquery');
    }
}

So the final output its just this:所以最终的输出就是这样:

Unexpected token < in JSON at position 0(…)

I don't know why the post returns the whole HTML, i'm posting my data as objects, what should i do?我不知道为什么帖子会返回整个 HTML,我将我的数据作为对象发布,我该怎么办?

数据帖子包含 JSON 对象,并且您获得了对象的一个​​元素,如下所示:

$this->input->post('your_post_element');

1) You have to recheck what is inside this variable 1)您必须重新检查此变量中的内容

data: {data: data},

2) You had set 2)你已经设置

contentType: 'application/json'

so you have to return the contents in JSON format and also should set content type to JSON in the response page所以你必须以 JSON 格式返回内容,并且还应该在响应页面中将内容类型设置为 JSON

$this->output
->set_content_type('application/json')
->set_output(json_encode($_POST));

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

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