duanmengmiezen8855 2018-05-24 16:58
浏览 76
已采纳

CI中的Ajax问题。 获取数据但JQuery显示错误

I have the following code

<?php
$arr=array();
$arr=$this->session->ques;
if(isset($this->session->username))
{
 echo '<div id="instruction">
      Welcome to Exam
     </div>';
 echo '<div id="questdiv">
       Start Now
     </div>';   
}
?>
$(document).ready(function(){
$("#questdiv").click(function()
{
    $("#instruction").hide();
    $.ajax({
      type:"post",
      url:"<?php echo base_url();?>"+"candidate/nextques",
      dataType:"json",
      data:{val: <?php echo json_encode($arr);?>},
      success:function(res){
      if(res)
      {
       $("#questdiv").append('<span>'+res[0]['question']);
       $("#questdiv").append('<span>'+res[0]['option1']);
       $("#questdiv").append('<span>'+res[0]['column2']);
       $("#questdiv").append('<span>'+res[0]['column3']);
       $("#questdiv").append('<span>'+res[0]['column4']);
      }
    },
    error: function(res,status,error) {
        var err = res.responseText;
      alert(res.Message);
      alert(status);
      alert(error);
    }
    }); 
  });   
});
</script>

In this code, I have two divs. I want that on clicking the div questdiv, AJAX should fire, get the values from controller-model and append some children inside the div.

Although data is fetched from the database (as shown by the network debugging), but preview also shows the above two divs due to which I am getting JSON parsing error.

[{"sno":"1","examcode":"PHP-101","question":"What is PHP?","image":"1-1.jpg","option1":"scripting language","image1":"1-2.jpg","option2":"programming language","image2":"1-3.jpg","option3":"both","image3":"1-4.jpg","option4":"none","image4":"1-5.jpg","correctans":"d58abf157fec3d16bf921e97966c9e57"}]
Welcome to Exam
Start Now

I am not able to figure out why I am getting parsing error when from the controller, I am getting valid JSON data only?

And if Welcome to Exam and Start Exam are creating problems, then how to solve it?

  • 写回答

1条回答 默认 最新

  • drn61317 2018-05-25 03:02
    关注

    You should have separate controller methods, one that renders the page with divs, and one that only returns your json object. They way you are doing it, you won't have a valid json object because the divs are rendering in there as well (because it is essentially re-rendering the page on ajax call).

    Example:

    class Some_controller {
    
        public function index() {
            $this->load->view('someview');
        }
    
        public function json() {
            $array = array('123', '456');
            echo json_encode($array);
        }
    
    }
    

    Someview:

    <script>
        $(document).ready(function () {
            $('#doAction').on('click', function (e) {
                e.preventDefault();
                $.ajax({
                    type: "get",
                    url: "<?php echo base_url(); ?>some_controller/json",
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                    }
                });
            });
        });
    </script>
    <div><a id="doAction" href="javascript:void()">Some div</a></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题