我们如何检查控制器中由代码点火器中的ajax发送的数据(没有响应)



我想将数组数据从javascript发送到视图页面。。但是数组没有被传递到视图页面。代码描述如下:

//数组从此页面传递

<input type="button" name="b">
<script src="<?php echo base_url().'assets/js/' ?>jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("input[type='button']").click(function(){
question_array=[];
question_array['what is our nationality?']='Bangladeshi';
question_array['what is our national fish?']='Ilish';
number=10;
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "admin/test",
dataType: 'json',
data: {question_array: question_array,number:number},
success: function(result) {
if (result) {
window.location="<?php echo base_url().'admin/test_page'; ?>";
}
}
});
});
});
</script>

`

在我的控制器中::

public function test()
{
$question_array = $this->input->post('question_array');
$number         = $this->input->post('number');
$this->session->set_flashdata('number', $number);
$this->session->set_flashdata('question_array', $question_array);
echo json_encode($number, $question_array);
}
function test_page()
{
$this->load->view('admin/test_page');
}

//我发送数组的页面(未找到Question_array(

$number         = $this->session->flashdata('number');
$question_array = $this->session->flashdata('question_array');
echo $number;
print_r($question_array);

希望这将帮助您:

将数组转换为object并删除数组键之间的空格,如下所示:

<script type="text/javascript">
$(function(){
$("input[type='button']").click(function(){
var question_array = [];
question_array['what-is-our-nationality'] = 'Bangladeshi';
question_array['what-is-our-national-fish'] = 'Ilish';
var number = 10;
var obj = $.extend({}, question_array);
jQuery.ajax({
type: "POST",
url: "<?php echo base_url('admin/test'); ?>",
dataType: 'json',
data: {question_array : obj, number : number},
success: function(result) {
if (result)
{
window.location="<?php echo base_url().'admin/test_page';?>";
}
}
});
});
});
</script>

你最好试试JSON.stringify来解决这个问题,

jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "admin/test",
dataType: 'json',
data: JSON.stringify({question_array: question_array,number:number}),
success: function(result) {
if (result){
window.location="<?php echo base_url().'admin/test_page'; ?>";
}
}
});

我希望下面的代码能对您有所帮助。您需要使用stringify发布字符串i更正JSON格式。服务器将接收json并将其解码为对象。

function send(){
var url = "http://localhost/some_url_here";
var data = JSON.stringify({
Bangladeshi: 'what is our nationality?',
Ilish: 'what is our national fish?'
});          
$.ajax({
url: url,
type: "POST",
dataType: "json",
data: data,
}).done(function(output){
alert('output is '+ JSON.stringify(output));
});

}

function receive() 
{
$result = '';
$stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
$request = json_decode($stream_clean);
$Bangladeshi = $request->Bangladeshi;
$Ilish = $request->Ilish;
$result = array("status" => 200, "id" => $Bangladeshi+$Ilish);
header('Content-Type: application/json');
echo json_encode($result);
}

相关内容

  • 没有找到相关文章

最新更新