我在尝试将数据发布到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 );
}
});
控制器| Dashboard.php - > class:Dashboard,function:index
atm我正在尝试用它来查看是否有发布的内容,但没有!
if($_POST) {
print_r($_POST);
die();
}
所以我的整个控制器看起来像这样......
<?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');
}
}
所以最终输出就是这样:
Unexpected token < in JSON at position 0(…)
我不知道为什么帖子会返回整个HTML,我将我的数据作为对象发布,我该怎么办?
答案 0 :(得分:0)
数据帖子包含JSON对象,你得到了一个像这样的对象元素:
SELECT date, amount,
AVG(amount) OVER(ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW) AS AvAmount
FROM a
ORDER BY date
答案 1 :(得分:0)
1)您必须重新检查此变量中的内容
data: {data: data},
2)你已经设置了
contentType: 'application/json'
所以你必须以JSON格式返回内容,并且还应该在响应页面中将内容类型设置为JSON
$this->output
->set_content_type('application/json')
->set_output(json_encode($_POST));